游标的问题
declare @qureyStr
set @qureyStr= 'SELECT id,els from Tproduct '
declare @crsEls cursor
set @crsEls = Cursor for
exec(@qureyStr)
运行这些代码时总是提示 exec附近有错误,我想问问游标是不是不能这么来用。
注:我的@qureyStr时要根据条件来构造一个查询语句,所以用到了exec
------解决方案--------------------declare @qureyStr varchar(1000)
set @qureyStr= 'declare cur_a cursor for SELECT id,els from Tproduct '
exec(@qureyStr)
declare @crsEls cursor
set @crsEls = cur_a
open @crsEls
fetch next from @crsEls
close @crsEls
deallocate @crsEls
------解决方案--------------------用exec执行可以定义在内部,但这样不没有必要再赋给游标变量了,可以直接操作cu
declare @qureyStr varchar(100)
set @qureyStr= 'declare cu cursor for SELECT id,els from Tproduct '
exec(@qureyStr)
declare @crsEls cursor
set @crsEls = cu
open @crsEls
fetch next from @crsEls
close @crsEls
deallocate @crsEls