日期:2014-05-18  浏览次数:20591 次

没有这种语法吗?
create   procedure   procTest1
        @field_name   nvarchar(20),
        @value   nvarchar(20),
        @where_name   nvarchar(20),
        @tbl_name   nvarchar(30),
        @curOut   cursor   varying   output
as  
        declare   @str   varchar(1000)
        select   @str= 'select   '+@field_name+ '   from   '+@tbl_name+
                                                          '   where   '+@where_name+ '= '+@value
        set   @curOut=cursor   for   @str
        open   @curOut
go

--我想返回一个游标.而我又想用动态Sql.怎么写?

------解决方案--------------------
/*
怎么可能不行
*/

create procedure procTest1
@field_name nvarchar(20)= 'name ',
@value nvarchar(20)=1,
@where_name nvarchar(20)= 'id ',
@tbl_name nvarchar(30)= 'sysobjects ',
@curOut cursor varying output
as
--declare @str varchar(1000)
declare @str nvarchar(2000)
--select @str= 'select '+@field_name+ ' from '+@tbl_name+ ' where '+@where_name+ '= '+@value
select @str=N 'set @curOut = cursor for select '+@field_name+N ' from '+@tbl_name+N ' where '+@where_name+N '= '+@value+char(10)+N 'open @curOut '
exec sp_executesql @str, N '@curOut cursor output ', @curOut output
--set @curOut=cursor for @str
--open @curOut
go


declare @curOut cursor
exec procTest1 @curOut=@curOut output
fetch next from @curOut
close @curOut
deallocate @curOut
/*
name
---------
sysobjects
*/

drop procedure procTest1