SQL里的游标是什么?如何将表名,列名,还有查询条件作为参数传进存储过程,要有代码和说明

来源:百度知道 编辑:UC知道 时间:2024/06/08 19:40:43
2楼有用到游标吗?

fetch next from cc into @ctemp 这句解释下,谢谢,看的不很明白
现在我已经会将这三个条件传入存储过程了,但没用到游标,不知道为什么看到别的存储过程要用
游标?有什么好处?

回答好的可以加分

create proc spname
@pctable varchar(20),--表名
@pccol varchar(20)--列名
as
begin
exec('select ' + @pccol + ' from ' + @pctable) --演示根据参数查询
/*游标演示
declare @ctemp int
declare cc cursor for select id from table --定义table的id列作为游标
open cc --打开游标
fetch next from cc into @ctemp --游标赋给变量
while @@fetch_status = 0 --如果游标存在
begin
print @ctemp
fetch next from cc into @ctemp --跳到下个游标
end

close cc --关闭游标
deallocate cc --释放游标
*/
end

----------------------------
declare cc cursor for select id from table --定义table的id列作为游标
open cc --打开游标
fetch next from cc into @ctemp --游标赋给变量

对于上面三句话的举例:
假设有表table,内容如下:
id name
1 张三
2 李四
3 王五
4 赵六

那么:declare cc cursor for select id from table --把table的第一列id定义为游标(cc)
定义了以后,游标存在了,但是为空。所以把第一行的id赋给cc。
fetch next from cc