SQL Server不接受INTO语句中的DECLARE

时间:2014-09-17 19:15:01

标签: sql-server-2008 while-loop insert-into

我对SQL Server有些新意,并尝试在INTO语句中使用while循环,但由于某种原因,SQL Server不允许我DECLARE我的变量INTO {1}}声明......似乎没有/ while循环,但是我需要能够DECLARE并定义我的变量以在while循环中使用...任何想法?

--Calculations portion of query
SELECT 
    FNL.* 
INTO 
    #Final_Calc 
FROM 
    (
     DECLARE @i INT
     SET @i = 0

     WHILE (@StartDte + @i <= @EndDte)
     BEGIN
          SET @i = @i + 1       

          SELECT 
             YEAR(ACD.WorkDte) AS WorkDte, 
             'LOB - ALL ACM' AS RptType, 
             'Month' AS RptLvl
          FROM 
             #FinalResults ACD 
          WHERE 
             ACD.WorkDte BETWEEN @StartDte AND @StartDte + @i 
          GROUP BY 
             Year(ACD.WorkDte), ACD.LOB
     END
     GO) fnl

所以,它不喜欢DECLARE @i INT部分......

0 个答案:

没有答案