谁知道这里有什么问题?

时间:2013-07-17 15:46:35

标签: sql sql-server cursor

BEGIN
OPEN DQC_Cursor1 ; 
LOOP

FETCH DQC_Cursor1 INTO @table_name1 ; 
EXIT when DQC_Cursor1%notfound;
EXEC Incorporate @table_name = @table_name1,
    @historical_table_name = Replace(
        @table_name1,
        'Temp',
        'Historical'
    ) ;

END EXEC;
END LOOP;


CLOSE DQC_Cursor1;
commit;
END;

我得到的错误消息如下。我的语法也接近错误;当我将鼠标悬停在带下划线的错误上时,期待对话。我已经写出了存储过程。谁知道这里出了什么问题?

Incorrect syntax near 'LOOP'.
Incorrect syntax near the keyword 'EXIT'.

1 个答案:

答案 0 :(得分:1)

我认为Incorporate是您拥有的存储过程。正如评论所示,您对光标语法有点混淆,阅读documentation将会有很大帮助。但在这种特定情况下,请尝试:

declare @table_name1 varchar(max);

declare DQC_Cursor1 cursor
for
    select Table_Name
    from TableNames;

open DQC_Cursor1
fetch next from DQC_Cursor1 into @table_name1
while @@FETCH_STATUS = 0 
    begin
        exec Incorporate @table_name = @table_name1,
                @historical_table_name = replace(@table_name1,'Temp','Historical')
    end
close DQC_Cursor1;
deallocate DQC_Cursor1;