declare   @cursor_name   varchar(36) 
select   @cursor_name   =   newid() 
declare @ckbm varchar(20)
exec('  DECLARE ['+@cursor_name + '] CURSOR FOR select f_ckbm from tbda_ck') 
exec ('open ['+ @cursor_name+']' )
exec('fetch from ['+ @cursor_name +'] into @ckbm') 
while @@fetch_status = 0 
begin
  print @ckbm
 exec('fetch from ['+ @cursor_name +'] into @ckbm') 
end
exec ('close ['+ @cursor_name+']' )
exec ('deallocate ['+ @cursor_name+']')
------------
服务器: 消息 137,级别 15,状态 1,行 1
必须声明变量 '@ckbm'。

解决方案 »

  1.   

    --我用的是系统表sysobjects来做的测试,前30条数据的name
    DECLARE @cursor_name nvarchar(200) select @cursor_name = 't_cursor',@sql =''declare @ckbm Nvarchar(20)
    SET @sql = @sql + ' declare  '+@cursor_name+' cursor for select top 20 name from sysobjects'
    SET @sql = @sql + ' open  '+@cursor_name+' '
    SET @sql = @sql + ' fetch NEXT from '+@cursor_name+' into @ckbm '
    SET @sql = @sql + 'while @@fetch_status = 0 '
    SET @sql = @sql + 'begin '
    SET @sql = @sql + 'print @ckbm '
    SET @sql = @sql + 'fetch NEXT from '+@cursor_name+' into @ckbm '
    SET @sql = @sql + 'end '
    SET @sql = @sql + 'close '+@cursor_name+' '
    SET @sql = @sql + 'deallocate '+@cursor_name+'  '
     EXEC sp_executesql @sql,
                        N'@ckbm nvarchar(20)=null',@ckbm--结果
    sysobjects
    sysindexes
    syscolumns
    systypes
    syscomments
    sysfiles1
    syspermissions
    sysusers
    sysproperties
    sysdepends
    sysreferences
    sysfulltextcatalogs
    sysindexkeys
    sysforeignkeys
    sysmembers
    sysprotects
    sysfulltextnotify
    sysdatabases
    sysperfinfo
    sysprocesses
      

  2.   

    --刚才少了一个DECLARE @sql NVARCHAR(4000)DECLARE @sql NVARCHAR(4000)
     DECLARE @cursor_name nvarchar(200) select @cursor_name = 't_cursor',@sql =''declare @ckbm Nvarchar(20)
    SET @sql = @sql + ' declare  '+@cursor_name+' cursor for select top 20 name from sysobjects'
    SET @sql = @sql + ' open  '+@cursor_name+' '
    SET @sql = @sql + ' fetch NEXT from '+@cursor_name+' into @ckbm '
    SET @sql = @sql + 'while @@fetch_status = 0 '
    SET @sql = @sql + 'begin '
    SET @sql = @sql + 'print @ckbm '
    SET @sql = @sql + 'fetch NEXT from '+@cursor_name+' into @ckbm '
    SET @sql = @sql + 'end '
    SET @sql = @sql + 'close '+@cursor_name+' '
    SET @sql = @sql + 'deallocate '+@cursor_name+'  '
     EXEC sp_executesql @sql,
                        N'@ckbm nvarchar(20)=null',@ckbm
      

  3.   

    declare @cursor_name varchar(36),@str nvarchar(4000)
    select  @cursor_name=newid() 
    DECLARE @ckbm varchar(20)exec('DECLARE ['+@cursor_name + '] CURSOR FOR select f_ckbm from tbda_ck')exec('open ['+ @cursor_name+']')set @str=N'fetch from ['+ @cursor_name +'] into @ckbm'exec sp_executesql @str,N'@ckbm varchar(20) out',@ckbm out
    while @@fetch_status = 0 
    begin
        print @ckbm
        exec sp_executesql @str,N'@ckbm varchar(20) out',@ckbm out
    endexec('close ['+ @cursor_name+']')
    exec('deallocate ['+ @cursor_name+']')
      

  4.   

    declare @cursor_name varchar(36),@str nvarchar(4000)
    select  @cursor_name=newid() 
    DECLARE @ckbm varchar(20)exec('DECLARE ['+@cursor_name + '] CURSOR FOR select f_ckbm from tbda_ck')exec('open ['+ @cursor_name+']')set @str=N'fetch from ['+ @cursor_name +'] into @ckbm'exec sp_executesql @str,N'@ckbm varchar(20) out',@ckbm out
    while @@fetch_status = 0 
    begin
        print @ckbm
        exec sp_executesql @str,N'@ckbm varchar(20) out',@ckbm out
    endexec('close ['+ @cursor_name+']')
    exec('deallocate ['+ @cursor_name+']')
      

  5.   

    declare   @cursor_name   varchar(36) 
    select   @cursor_name   =   newid() 
    declare @ckbm varchar(20)
    exec('  DECLARE ['+@cursor_name + '] CURSOR FOR select f_ckbm from tbda_ck') 
    exec ('open ['+ @cursor_name+']' )
    exec('fetch from ['+ @cursor_name +'] into'+ @ckbm) 
    while @@fetch_status = 0 
    begin
      print @ckbm
     exec('fetch from ['+ @cursor_name +'] into'+ @ckbm) 
    end
    exec ('close ['+ @cursor_name+']' )
    exec ('deallocate ['+ @cursor_name+']')