游标取数存放在哪里?

来源:百度知道 编辑:UC知道 时间:2024/06/01 03:43:39
我写了一个程序,是在客户机上运行的,服务器上存放了数据库,我程序要运行查询数据的话都需要从服务器中取数据,我程序用了大量的游标,现在怀疑用游标影响速度。请问一下游标用select取的数据存放在哪里?有fecth取的数据又存放在哪里?
你没有回答到重点,没有回答我提的问题。其实这个答案我已经知道了,定义游标select查询的数据存放在数据库的临时表里面,即存在在服务器的数据库中,用fetch取的数据是存放在本机内存的,即客户机内存。如果在客户机上大量使用游标,的确会使程序慢!

例6.查询每个系年龄最大的两个学生信息,列出系名、学生姓名和
--年龄。
declare @sd1 char(20), @sn1 char(10), @sg1 int
declare @sd2 char(20), @sn2 char(10), @sg2 int
declare cur_sage cursor for
select sdept,sname,sage
from student order by sdept,sage desc
open cur_sage
--取第一行,一定是需要的数据
fetch next from cur_sage into @sd1, @sn1, @sg1
while @@fetch_status = 0
begin
print @sd1 + ' ' + @sn1 + ' ' + cast(@sg1 as char(2))
--取第2行
fetch next from cur_sage into @sd2, @sn2, @sg2
print @sd2 + ' ' + @sn2 + ' ' + cast(@sg2 as char(2))
while @sd1 = @sd2 and @@fetch_status = 0
fetch next from cur_sage into @sd2, @sn2, @sg2
if @@fetch_status = 0
begin
set @sd1 = @sd2
set @sn1 = @sn2
set @sg1 = @sg2
end
end
close cur_sage
deallocate cur_sage
关于这个例题,在提取第二行以后不是很明白,

while @@fetch_status = 0
begin
print @sd1 + ' ' + @s