当前位置: 代码迷 >> Sql Server >> 小弟我对游标不熟,高手来看看为什么打印不出来
  详细解决方案

小弟我对游标不熟,高手来看看为什么打印不出来

热度:96   发布时间:2016-04-27 20:19:52.0
我对游标不熟,高手来看看为什么打印不出来
我把一个游标写在一个存储过程中.要实现的是把Users字段里的所有字段取出来,[email protected]?想通过print   @SqlStr把这个代码打印出来.但是执行成功就是没有显示?
if   exists(select   name   from   sysobjects   where   name= 'test '   and   type= 'p ')
drop   proc   test
go

create   proc   test
@SearchStr   varchar(255)
as
declare   @SqlStr   varchar(1000)
declare   my_cursor   cursor   scroll   for   select   name   from   syscolumns   where   id=object_id( 'Users ')
open   my_cursor
declare   @name   sysname
fetch   next   from   my_cursor   into   @name
while(@@fetch_status=0)
    begin
        /*print   '字段:   '   +   @name*/
        set   @[email protected]+ '   '[email protected]+ '   like   % '[email protected]+ '%   '
        fetch   next   from   my_cursor   into   @name
    end
/*fetch   first   from   my_cursor   into   @name*/
/*print   @name*/
close   my_cursor
deallocate   my_cursor
print   @SqlStr

------解决方案--------------------
[email protected] ' '
------解决方案--------------------
if exists(select name from sysobjects where name= 'test ' and type= 'p ')
drop proc test
go

create proc test
@SearchStr varchar(255)
as
declare @SqlStr varchar(1000)
set @SqlStr= ' ' ---这个地方先赋个初始值
declare my_cursor cursor scroll for select name from syscolumns where id=object_id( 'Users ')
open my_cursor
declare @name sysname
fetch next from my_cursor into @name
while(@@fetch_status=0)
begin
/*print '字段: ' + @name*/
set @[email protected]+ ' '[email protected]+ ' like % '[email protected]+ '% '
fetch next from my_cursor into @name
end
/*fetch first from my_cursor into @name*/
/*print @name*/
close my_cursor
deallocate my_cursor
print @SqlStr
------解决方案--------------------
[email protected] 給初值
set @SqlStr = ' '
------解决方案--------------------
declare @SqlStr varchar(1000)
@sqlstr没有赋初始值
应该加上:set @sql= ' '
  相关解决方案