Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6234
  • 博文数量: 7
  • 博客积分: 240
  • 博客等级: 二等列兵
  • 技术积分: 75
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-08 14:23
文章分类
文章存档

2011年(1)

2009年(6)

我的朋友
最近访客

分类: 数据库开发技术

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 @qxcode varchar(20)
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 

阅读(381) | 评论(1) | 转发(0) |
0

上一篇:水晶报表

下一篇:SQL Server函数大全[转]

给主人留下些什么吧!~~

chinaunix网友2009-07-28 18:30:11

不错!