Here is my code:
use actg
--NEED PARAMETERS FOR CURRENT MONTH AND NUMBER OF MONTHS
DECLARE @CurrMth NUMERIC(18,0);
SET @CurrMth = 9;
DECLARE @CurrMthName VARCHAR(3);
IF @CurrMth = 1
BEGIN
SET @CurrMthName = 'Jan'
END
ELSE if @CurrMth = 2
BEGIN
SET @CurrMthName = 'Feb'
END
ELSE IF @CurrMth = 3
BEGIN
SET @CurrMthName = 'Mar'
END
ELSE IF @CurrMth = 4
BEGIN
SET @CurrMthName = 'Apr'
END
ELSE IF @CurrMth = 5
BEGIN
SET @CurrMthName = 'May'
END
ELSE IF @CurrMth = 6
BEGIN
SET @CurrMthName= 'Jun'
END
ELSE IF @CurrMth = 7
BEGIN
SET @CurrMthName= 'Jul'
END
ELSE IF @CurrMth = 8
BEGIN
SET @CurrMthName= 'Aug'
END
ELSE IF @CurrMth = 9
BEGIN
SET @CurrMthName= 'Sep'
END
ELSE IF @CurrMth = 10
BEGIN
SET @CurrMthName= 'Oct'
END
ELSE IF @CurrMth = 11
BEGIN
SET @CurrMthName= 'Nov'
END
ELSE IF @CurrMth = 12
BEGIN
SET @CurrMthName= 'Dec'
END
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 to current month
update NITable
SET CurrentMonth = A.SumOf + @CurrMthName
From @NITable As NITable
Inner Join (
select [Actg_FPG_Reg_All_Deps_Report_NI].Reg,
[Actg_FPG_Reg_All_Deps_Report_NI].dept,
CAST(SUM(@CurrMthName)AS NUMERIC(18,0)) As SumOf + @CurrMthName
from [Actg_FPG_Reg_All_Deps_Report_NI]
Group By [Actg_FPG_Reg_All_Deps_Report_NI].Reg,
[Actg_FPG_Reg_All_Deps_Report_NI].dept
) As A
On NITable.Reg = A.Reg
And NITable.Dept = A.Dept
The problem I have is with the last update. The usage of @CurrMthName is not working and I have googled this thing to death to try to find out why. Can anyone help me on this? Any help is appreciated. The error message I get is that the SQL Server does not like the '+'.
use actg
--NEED PARAMETERS FOR CURRENT MONTH AND NUMBER OF MONTHS
DECLARE @CurrMth NUMERIC(18,0);
SET @CurrMth = 9;
DECLARE @CurrMthName VARCHAR(3);
IF @CurrMth = 1
BEGIN
SET @CurrMthName = 'Jan'
END
ELSE if @CurrMth = 2
BEGIN
SET @CurrMthName = 'Feb'
END
ELSE IF @CurrMth = 3
BEGIN
SET @CurrMthName = 'Mar'
END
ELSE IF @CurrMth = 4
BEGIN
SET @CurrMthName = 'Apr'
END
ELSE IF @CurrMth = 5
BEGIN
SET @CurrMthName = 'May'
END
ELSE IF @CurrMth = 6
BEGIN
SET @CurrMthName= 'Jun'
END
ELSE IF @CurrMth = 7
BEGIN
SET @CurrMthName= 'Jul'
END
ELSE IF @CurrMth = 8
BEGIN
SET @CurrMthName= 'Aug'
END
ELSE IF @CurrMth = 9
BEGIN
SET @CurrMthName= 'Sep'
END
ELSE IF @CurrMth = 10
BEGIN
SET @CurrMthName= 'Oct'
END
ELSE IF @CurrMth = 11
BEGIN
SET @CurrMthName= 'Nov'
END
ELSE IF @CurrMth = 12
BEGIN
SET @CurrMthName= 'Dec'
END
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 to current month
update NITable
SET CurrentMonth = A.SumOf + @CurrMthName
From @NITable As NITable
Inner Join (
select [Actg_FPG_Reg_All_Deps_Report_NI].Reg,
[Actg_FPG_Reg_All_Deps_Report_NI].dept,
CAST(SUM(@CurrMthName)AS NUMERIC(18,0)) As SumOf + @CurrMthName
from [Actg_FPG_Reg_All_Deps_Report_NI]
Group By [Actg_FPG_Reg_All_Deps_Report_NI].Reg,
[Actg_FPG_Reg_All_Deps_Report_NI].dept
) As A
On NITable.Reg = A.Reg
And NITable.Dept = A.Dept
The problem I have is with the last update. The usage of @CurrMthName is not working and I have googled this thing to death to try to find out why. Can anyone help me on this? Any help is appreciated. The error message I get is that the SQL Server does not like the '+'.