Hello Experts,
I came through a problem. I am using SSMS 2014. i need to create a view for getting month list of previous three years. For that i am using below code . But its showing error:-incorrect syntax near Declare. Because i think in view we cannot use DECLARE and SET keyword .
So please suggest me any solution for the problem.
DECLARE @StartDATE DATETIME;
SET @StartDATE = '01-01-2000';
WITH Tens (N) AS
(
SELECT 0 N UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0
),
Thousands (N) AS
(
SELECT t1.N FROM Tens t1 CROSS JOIN Tens t2 CROSS JOIN Tens t3
),
Tally AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) N FROM Thousands
)
SELECT MonthNum =
CASE T.N % 12
WHEN 0 THEN 12
ELSE T.N % 12
END
, DATEPART(YY,DATEADD(M,N-1,@StartDATE)) [YEAR]
, DATENAME(MM,DATEADD(M,N-1,@StartDATE)) [MONTH]
FROM Tally T
WHERE DATEPART(YY,DATEADD(M,N-1,@StartDATE)) <= YEAR(GETDATE()) and DATEPART(YY,DATEADD(M,N-1,@StartDATE)) >= YEAR(GETDATE())-3;
Quick responses will be highly appreciable.
Thanks in advance ,
Manita Raj