create proc csearch
@tablename sysname,
@condition varchar(2000)
as
declare @sqlstr varchar(4000)
set @sqlstr = 'declare cur_result cursor for select a,b,c from ' + @tablename + ' where ' + @condition
exec executesql @sqlstr
open cur_result
......
运行时报错:
没有名为cur_result的cursor
有什么办法解决吗?
分享到:
------解决方案--------------------
create proc csearch
@tablename sysname,
@condition varchar(2000)=NULL
as
DECLARE @WHERE VARCHAR(MAX)
SET @WHERE=''
SELECT @condition=ISNULL(@condition,'')
IF @condition>''
SET @WHERE=' WHERE @condition'
BEGIN
declare @sqlstr nvarchar(4000)
set @sqlstr = 'declare cur_result cursor for select a,b,c from ' + @tablename + '+@WHERE+'
SET @sqlstr=@sqlstr+' open cur_result .....'
EXEC sp_executesql @sqlstr,N'@condition varchar(2000),@condition
end
+1
你游标的操作语句要放在execute sp_excutesql的第一个参数里面执行。或者用临时表。 ------解决方案-------------------- alter proc csearch1
@tablename sysname,
@condition varchar(100)=null
as
begin
declare @sqlstr varchar(4000)
set @sqlstr = 'declare cur_result cursor for select timestr from ' + @tablename + ' where ' + @condition exec(@sqlstr)
open cur_result
close cur_result
deallocate cur_result