Here is what I am trying to do. I have a table variable :
DECLARE @NITable table
(
Reg Varchar(1),
Dept Varchar(3),
CurrentMonth Numeric(18,0),
CurrentYTD Numeric(18,0),
CurrentAvg Numeric(18,0),
PriorYTD Numeric(18,0),
PriorAvg Numeric(18,0)
)
The reg and dept is populated from two tables. Current year reg and dept is first inserted and then previous year's reg and dept is inserted using an unmatched query. I need to update Current Month with the net income from the current year's data matching the current year table's net income by reg and dept. Therein is my problem. I tried to set up the following query:
update @NITable
SET CurrentMonth =(select CAST(SUM([Sep])AS NUMERIC(18,0))from
[Actg_FPG_Reg_All_Deps_Report_NI]
where @NITable.reg = [Actg_FPG_Reg_All_Deps_Report_NI].Reg and @NITable.Dept =
[Actg_FPG_Reg_All_Deps_Report_NI].dept )
SQL Server 2005 is throwing an error on the where clause that @NITable is not declared. Here is all of my code:
DECLARE @NITable table
(
Reg Varchar(1),
Dept Varchar(3),
CurrentMonth Numeric(18,0),
CurrentYTD Numeric(18,0),
CurrentAvg Numeric(18,0),
PriorYTD Numeric(18,0),
PriorAvg Numeric(18,0)
)
Declare @NIGrouped Table
(
Reg Varchar(1),
Dept Varchar(3)
)
insert into @NIGrouped
(
Reg,
Dept
)
select Reg, Dept from Actg_FPG_Reg_All_Deps_Report_NI
group by Reg, Dept
insert into @NIGrouped
(
Reg,
Dept
)
select Reg, Dept from Actg_FPG_Reg_All_Deps_Report_2010
group by Reg, Dept
insert into @NITable
(
Reg,
Dept
)
select Reg, Dept from @NIGrouped
group by Reg, Dept
update @NITable
SET CurrentMonth =(select CAST(SUM([Sep])AS NUMERIC(18,0))from
[Actg_FPG_Reg_All_Deps_Report_NI]
where @NITable.reg = [Actg_FPG_Reg_All_Deps_Report_NI].Reg and @NITable.Dept =
[Actg_FPG_Reg_All_Deps_Report_NI].dept )
I am not sure if the last update query is even right or why I am getting the error message. Can someone help me on these two items? Any help is greatly appreciated. Thanks.
DECLARE @NITable table
(
Reg Varchar(1),
Dept Varchar(3),
CurrentMonth Numeric(18,0),
CurrentYTD Numeric(18,0),
CurrentAvg Numeric(18,0),
PriorYTD Numeric(18,0),
PriorAvg Numeric(18,0)
)
The reg and dept is populated from two tables. Current year reg and dept is first inserted and then previous year's reg and dept is inserted using an unmatched query. I need to update Current Month with the net income from the current year's data matching the current year table's net income by reg and dept. Therein is my problem. I tried to set up the following query:
update @NITable
SET CurrentMonth =(select CAST(SUM([Sep])AS NUMERIC(18,0))from
[Actg_FPG_Reg_All_Deps_Report_NI]
where @NITable.reg = [Actg_FPG_Reg_All_Deps_Report_NI].Reg and @NITable.Dept =
[Actg_FPG_Reg_All_Deps_Report_NI].dept )
SQL Server 2005 is throwing an error on the where clause that @NITable is not declared. Here is all of my code:
DECLARE @NITable table
(
Reg Varchar(1),
Dept Varchar(3),
CurrentMonth Numeric(18,0),
CurrentYTD Numeric(18,0),
CurrentAvg Numeric(18,0),
PriorYTD Numeric(18,0),
PriorAvg Numeric(18,0)
)
Declare @NIGrouped Table
(
Reg Varchar(1),
Dept Varchar(3)
)
insert into @NIGrouped
(
Reg,
Dept
)
select Reg, Dept from Actg_FPG_Reg_All_Deps_Report_NI
group by Reg, Dept
insert into @NIGrouped
(
Reg,
Dept
)
select Reg, Dept from Actg_FPG_Reg_All_Deps_Report_2010
group by Reg, Dept
insert into @NITable
(
Reg,
Dept
)
select Reg, Dept from @NIGrouped
group by Reg, Dept
update @NITable
SET CurrentMonth =(select CAST(SUM([Sep])AS NUMERIC(18,0))from
[Actg_FPG_Reg_All_Deps_Report_NI]
where @NITable.reg = [Actg_FPG_Reg_All_Deps_Report_NI].Reg and @NITable.Dept =
[Actg_FPG_Reg_All_Deps_Report_NI].dept )
I am not sure if the last update query is even right or why I am getting the error message. Can someone help me on these two items? Any help is greatly appreciated. Thanks.