游標Cursor的select_statement語句中如何使用變量
想從一個變量當中取得表名,然后作為游標當中查詢語句所用到的Table
具體代碼如下:select 語句中提示必需定義變量 @tablename ,我的目的就是必需用一個變量來代替表名,望高手指導啊!!
declare @dateid nvarchar(8)
declare @tablename nvarchar(20)
set @tablename = 'myTable '
DECLARE cur_Infor_1 CURSOR FOR
SELECT dateid FROM @tablename
OPEN cur_Infor_1
FETCH NEXT FROM cur_Infor_1 INTO @dateid
WHILE @@fetch_status=0
BEGIN
if(@dateid = '20070324 ')
print 'Success : ' + @dateid
else
print 'Failed : ' + @dateid
FETCH NEXT FROM cur_Infor_1 INTO @dateid
END
CLOSE cur_Infor_1
DEALLOCATE cur_Infor_1
------解决方案--------------------exec( 'DECLARE cur_Infor_1 CURSOR FOR
SELECT dateid FROM '+@tablename)