sql中的游标是什么?怎样用呢?

来源:百度知道 编辑:UC知道 时间:2024/06/15 16:45:49
请看下面的代码 谁能帮帮忙解释下由"????"标记的代码
Create Proc Pr_DeleteTable
as
declare @Table varchar(20)
declare cr_cursor cursor --1.定义游标
for select name from dbo.sysobjects where xtype='U' and status>0
--??????
open cr_cursor --2.打开游标
fetch From cr_cursor into @Table --3.提取游标
while @@fetch_status=0
begin
print @Table --执行打印操作
fetch next From cr_cursor into @Table --?????????
end;
close cr_cursor --4.关闭游标
deallocate cr_cursor --5.释放游标
还有就是这行代码怎样能看出他运行效果呢?我是初学者请各位大虾将详细点! 谢了!!

declare cr_cursor cursor --1.定义游标
for select name from dbo.sysobjects where xtype='U' and status>0
--?????? 这里是获取记录
fetch next From cr_cursor into @Table --??这里是用变量@Table保存获取到的select 【name】 from dbo.sysobjects where xtype='U' and status>0
name的值
fetch next From cr_cursor into @Table--这句话的完整意思是
将游标移动到下一条记录并将获取到是name值赋值给变量@Table
----------------------------------------------------------------------
给你一个例子 和说明 我看来几遍就学会游标了 下面是例子
---------------------------------------------------------------------
定义游标
Declare MyCursor Cursor For
Select Field1,Field2
From MyTable
Where (Field1 Like '%123%') And (Field2 = 'qqq') And (Field3 Is Not Null) And ......
Group By Field1,Field2
For Read Only
Open MyCursor

移动游标
fetch first from