我对游标不熟,高手来看看为什么打印不出来
我把一个游标写在一个存储过程中.要实现的是把Users字段里的所有字段取出来,一一加到@SqlStr中.实现泛搜索的一个SQL代码.最后要想通过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 @SqlStr=@SqlStr+ ' '+@name+ ' like % '+@SearchStr+ '% '
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
[解决办法]
将@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)
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 @SqlStr=@SqlStr+ ' '+@name+ ' like % '+@SearchStr+ '% '
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
[解决办法]
始咬@SqlStr 初值
set @SqlStr = ' '
[解决办法]
declare @SqlStr varchar(1000)
@sqlstr没有赋初始值
应该加上:set @sql= ' '