动态定义游标的方法

2008-02-23 07:43:43来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

  方法一:

  不知道多输出参数如何写:

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
end

exec('close [' @cursor_name ']')
exec('deallocate [' @cursor_name ']')

  方法二:

  能够实现多输出参数

  declare @sqlExec varchar(8000),@tableName varchar(255),@myvar varchar(20)
  set @tableName = 'Checkbag_info'
  set @sqlExec = 'declare cursor1 cursor for ' CHAR(13)
  set @sqlExec = @sqlExec ' select res_sort from ' @tableName
  exec(@sqlExec)
  open cursor1
  fetch next from cursor1 into @myvar
  while @@fetch_status = 0
  begin
  print @myvar
  fetch next from cursor1 into @myvar
  end
  close cursor1
  deallocate cursor1


标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: MySQL错误代码

下一篇: in和exists的区别和执行效率问题解析