示例 A. 使用简单游标和语法 打开该游标时所生成的结果集包括 pubs 数据库的 authors 表中的所有行和列。可以更新该游标,对该游标所做的所有更新和删除均在提取中表现出来。因为没指定 SCROLL 选项,FETCH NEXT 是唯一可用的提取选项。DECLARE authors_cursor CURSOR FOR SELECT * FROM authors OPEN authors_cursor FETCH NEXT FROM authors_cursor
create function myfun1 (@num int,@num1 int) returns @temp table (id int,rank int,ac int,nid int) as begin declare @a1 int,@a2 int,@a3 int,@a4 int,@a5 int set @a5 = 0 declare yb1 cursor local for select id,rank,ac,1 as id1 from table1 order by rank ASC,ac DESC --初始化游标 open yb1 --打开游标 fetch yb1 into @a1,@a2,@a3,@a4 --将游标中的字段付给变量 while @@fetch_status = 0 --判断语句,判断游标是否得到数据 begin set @a5 = @a5 + 1 --这里和游标没有关系 insert @temp (id,rank,ac,nid) --这里和游标没有关系 select @a1 as [id],@a2 as rank,@a3 as ac,@a5 as [newid] where @a5 > @num and @a5 < @num+@num1 --这里和游标没有关系 fetch yb1 into @a1,@a2,@a3,@a4 --给游标新的行 end close yb1 关闭游标 deallocate yb1 释放游标 return end
A. 使用简单游标和语法
打开该游标时所生成的结果集包括 pubs 数据库的 authors 表中的所有行和列。可以更新该游标,对该游标所做的所有更新和删除均在提取中表现出来。因为没指定 SCROLL 选项,FETCH NEXT 是唯一可用的提取选项。DECLARE authors_cursor CURSOR
FOR SELECT * FROM authors
OPEN authors_cursor
FETCH NEXT FROM authors_cursor
returns @temp table
(id int,rank int,ac int,nid int)
as
begin
declare @a1 int,@a2 int,@a3 int,@a4 int,@a5 int
set @a5 = 0
declare yb1 cursor local
for select id,rank,ac,1 as id1 from table1 order by rank ASC,ac DESC --初始化游标
open yb1 --打开游标
fetch yb1 into @a1,@a2,@a3,@a4 --将游标中的字段付给变量
while @@fetch_status = 0 --判断语句,判断游标是否得到数据
begin
set @a5 = @a5 + 1 --这里和游标没有关系
insert @temp (id,rank,ac,nid) --这里和游标没有关系
select @a1 as [id],@a2 as rank,@a3 as ac,@a5 as [newid] where @a5 > @num and @a5 < @num+@num1 --这里和游标没有关系
fetch yb1 into @a1,@a2,@a3,@a4 --给游标新的行
end
close yb1 关闭游标
deallocate yb1 释放游标
return
end