分类: 数据库开发技术
2009-07-28 16:28:00
一、游标定义
Declare Cursor_name Cursor
二、数据来源
For select ……
三、打开游标
Open Cursor_name
四、从游标中查找信息
Fetch
如:fetch next -------返回结果集中当前行的下一行,并增加当前行数为返回行行数。如果FETCH NEXT是第一次读取游标中数据,则返回结果集中的是第一行而不是第二行。
Fetch next from cursor_name into 变量名 -------给变量赋值。
五、关闭游标
CLOSE 或DEALLOCATE 游标。
六、例子
declare @InstallRate int
declare @yes_Setup int
create table #table
(
qxcode varchar(20),
InstallRate int
)
declare qx cursor
for select code from region where len(code)=6
open qx
declare @newcode varchar (20)
fetch next from qx into @newcode
while(@@Fetch_Status =0)
begin
select @yes_Setup=count(*) from BarInfo where nearily_time is not null and BarCode like @newCode+'%'
insert into #table(qxcode,InstallRate)values (@newcode,@yes_Setup)
fetch next from qx into @newcode
end
close qx
Deallocate qx
select * from #table
drop table #table