首页 技术 正文
技术 2022年11月17日
0 收藏 641 点赞 2,159 浏览 2445 个字
 if exists(select * from sys.objects where name='info_one')
drop table info_one
go
create table info_one
(
id int ,
iName varchar()
)
insert into info_one values(,'小明')
insert into info_one values(,'小红')
insert into info_one values(,'小白') declare @id int
declare @name varchar()
declare cursor1 cursor for --定义游标cursor1
select * from info_one --使用游标的对象(跟据需要填入select文)
open cursor1 --打开游标 fetch next from cursor1 into @id,@name --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中 while @@fetch_status= --判断是否成功获取数据
begin
update info_one set iname=iname+''
where id=@id --进行相应处理(跟据需要填入SQL文) fetch next from cursor1 into @id,@name --将游标向下移1行
end close cursor1 --关闭游标
deallocate cursor1 select * from info_one

@@fetch_status是MicroSoft SQL SERVER的一个全局变量

其值有以下三种,分别表示三种不同含义:【返回类型integer】0 FETCH 语句成功-1 FETCH 语句失败或此行不在结果集中-2 被提取的行不存在@@fetch_status值的改变是通过fetch next from实现的“FETCH NEXT FROM Cursor”

游标一般格式:
DECLARE 游标名称 CURSOR FOR SELECT 字段1,字段2,字段3,… FROM 表名 WHERE …
OPEN 游标名称
FETCH NEXT FROM 游标名称 INTO 变量名1,变量名2,变量名3,…
WHILE @@FETCH_STATUS=0
        BEGIN
                  SQL语句执行过程… …
                  FETCH NEXT FROM 游标名称 INTO 变量名1,变量名2,变量名3,…
        END
CLOSE 游标名称
DEALLOCATE 游标名称 (删除游标)

 例子:
/*
功能:数据库表格tbl_users数据
deptid userid username
1 100 a
1 101 b
2 102 c
要求用一个sql语句输出下面结果
deptid username
1 ab
2 c
[要求用游标实现设计: OK_008
时间: 2006-05
备注:无
*/
create table #Temp1(deptid int,userid int,username varchar()) --待测试的数据表
create table #Temp2(deptid int,username varchar()) --结果表
--先把一些待测试的数据插入到待测试表#Temp1中
insert into #Temp1
select ,,'a' union all
select ,,'b' union all
select ,,'d' union all
select ,,'f' union all
select ,,'c' union all
select ,,'a' union all
select ,,'e' union all
select ,,'y' union all
select ,,'e' union all
select ,,'t'
--
declare @deptid int,@username varchar()
--定义游标
declare Select_cursor cursor for
select deptid,username from #Temp1
open Select_cursor
fetch next from Select_cursor into @deptid,@username --提取操作的列数据放到局部变量中
while @@fetch_status= --返回被 FETCH 语句执行的最后游标的状态
/*
@@FETCH_STATUS =0 FETCH 语句成功
@@FETCH_STATUS =-1 FETCH 语句失败或此行不在结果集中
@@FETCH_STATUS =-2 被提取的行不存在
*/
begin
--当表#Temp2列deptid存在相同的数据时,就直接在列username上追加@username值
if(exists(select * from #Temp2 where deptid=@deptid ))
update #Temp2 set username=username +@username where deptid=@deptid
else
--插入新数据
insert into #Temp2 select @deptid,@username
fetch next from Select_cursor into @deptid,@username
end
close Select_cursor
deallocate Select_cursor
select * from #Temp2 --测试结果
Drop table #Temp1,#Temp2

游标一般格式:
DECLARE 游标名称 CURSOR FOR SELECT 字段1,字段2,字段3,… FROM 表名 WHERE …
OPEN 游标名称
FETCH NEXT FROM 游标名称 INTO 变量名1,变量名2,变量名3,…
WHILE @@FETCH_STATUS=0
        BEGIN
                  SQL语句执行过程… …
                  FETCH NEXT FROM 游标名称 INTO 变量名1,变量名2,变量名3,…
        END
CLOSE 游标名称
DEALLOCATE 游标名称 (删除游标)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,361
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853