这是我的查询
Declare @i int
Declare @Query nvarchar(max)
Set @i = 1
while(@i < 3)
begin
Select YEAR, MONTH
into #temp
from Toyota1
Set @Query = 'Select * into table'+ CASt(@i AS VARCHAR(10)) + 'from #temp'
Set @i = @i + 1
exec (@query)
Drop Table #temp
end
我得到这些错误:
第15层状态1,第2行,Msg 102
“ #temp”附近的语法不正确第15层状态1,第2行,Msg 102
'#temp'附近的语法不正确。
请帮助。
答案 0 :(得分:0)
可能是这样的:
DECLARE @i INT = 1
DECLARE @Query NVARCHAR(MAX);
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp;
SELECT TOP (0) [YEAR], [MONTH]
INTO #temp
FROM Toyota1;
WHILE(@i < 3)
BEGIN
INSERT #temp ([YEAR], [MONTH])
SELECT [YEAR], [MONTH]
FROM Toyota1
WHERE <some logic here that requires a loop>
SET @i += 1
END
SELECT *
INTO <TABLE>
FROM #temp;
DROP TABLE #temp;