Click here to Skip to main content
15,886,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
DECLARE @Totaltbl TABLE (
PeriodTTL decimal (11,2), YTDTTL decimal (11,2), JTDTTL decimal (11,2))

INSERT INTO @Totaltbl (PeriodTTL, YTDTTL, JTDTTL) SELECT subTTLPer=( Table2.col1 + Table2.col2 + Table2.col3), subTTLYTD=(Table2.col4 + Table2.col5 + Table2.col6), subTTLJTD=(Table2.col7 + Table2.col8 + Table2.col9)

FROM dbo.Table2

UPDATE @Totaltbl SET PeriodTTL = subTTLPer, YTDTTL = subTTLYTD, JTDTTL = subTTLJTD

Go

SELECT Table2.col_10, Table2.col_11, Table2.col_12

FROM dbo.Table2

UPDATE @Totaltbl SET PeriodTTL = col_10, YTDTTL = col_11, JTDTTL = col_12

Go

SELECT sumCurPerAdj = SUM(Table1.Col1), sumFYDamt = SUM(Table1.Col2), sumITDamt = SUM(Table1.Col3)

FROM dbo.Table1

UPDATE @RevTotalstbl SET PeriodTTL = sumCurPerAdj, YTDTTL = sumFYDamt, JTDTTL = sumITDamt


[edit]Code Block fixed - OriginalGriff[/edit]
Posted
Updated 28-Mar-11 21:03pm
v3
Comments
Om Prakash Pant 29-Mar-11 3:37am    
you are already provided the code to insert data in temp tables and do calculations. what is the issue that you are facing?

1 solution

1. You declare @Totaltbl as table variable (table variable != temp table ) and any variable can be accessed only in the batch that was created.

2. You update table @RevTotalstbl that is never declared

3. The code should look like
SQL
DECLARE @Totaltbl TABLE (
PeriodTTL decimal (11,2), YTDTTL decimal (11,2), JTDTTL decimal (11,2))
INSERT INTO @Totaltbl (PeriodTTL, YTDTTL, JTDTTL) SELECT subTTLPer=( Table2.col1 + Table2.col2 + Table2.col3), subTTLYTD=(Table2.col4 + Table2.col5 + Table2.col6), subTTLJTD=(Table2.col7 + Table2.col8 + Table2.col9)
FROM dbo.Table2
UPDATE @Totaltbl SET PeriodTTL = subTTLPer, YTDTTL = subTTLYTD, JTDTTL = subTTLJTD
--Go
SELECT Table2.col_10, Table2.col_11, Table2.col_12
FROM dbo.Table2
UPDATE @Totaltbl SET PeriodTTL = col_10, YTDTTL = col_11, JTDTTL = col_12
--Go
DECLARE @RevTotalstbl TABLE( COLUMN DEFINITIONS GO HERE)
SELECT sumCurPerAdj = SUM(Table1.Col1), sumFYDamt = SUM(Table1.Col2), sumITDamt = SUM(Table1.Col3)
FROM dbo.Table1
UPDATE @RevTotalstbl SET PeriodTTL = sumCurPerAdj, YTDTTL = sumFYDamt, JTDTTL = sumITDamt
 
Share this answer
 
Comments
technette 29-Mar-11 13:12pm    
Costica,

Thank you for responding. I still get an error on the following line 2 stating that I must declare a table variable.

Declare @Ttotaltbl TABLE (PeriodTTL decimal (11,2), YTDTTL decimal (11,2), JTDTTL decimal (11,2))


Msg 1087, Level 15, State 2, Line 2
Must declare the table variable "@Ttotaltbl".

INSERT INTO @Ttotaltbl (PeriodTTL, YTDTTL, JTDTTL)
Costica U 29-Mar-11 13:49pm    
Did you run only a portion of the script?
technette 29-Mar-11 14:49pm    
No I ran the entire script. I just replied with a copy of the line number where the error occurred.
Costica U 30-Mar-11 5:11am    
Please post the exact script.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900