SQL Cursor 基本用法
来源:互联网 发布:linux 进程状态日志 编辑:程序博客网 时间:2024/05/15 09:09
由于这个游标 执行一下就相当于SELECT一下 其效率不敢恭维也没做深入研究。
代码
1 table1结构如下
2 id int
3 name varchar(50)
4
5 declare @id int
6 declare @name varchar(50)
7 declare cursor1 cursor for --定义游标cursor1
8 select * from table1 --使用游标的对象(跟据需要填入select文)
9 open cursor1 --打开游标
10
11 fetch next from cursor1 into @id,@name --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中
12
13 while @@fetch_status=0 --判断是否成功获取数据
14 begin
15 update table1 set name=name+'1'
16 where id=@id --进行相应处理(跟据需要填入SQL文)
17
18 fetch next from cursor1 into @id,@name --将游标向下移1行
19 end
20
21 close cursor1 --关闭游标
22 deallocate cursor1
2 id int
3 name varchar(50)
4
5 declare @id int
6 declare @name varchar(50)
7 declare cursor1 cursor for --定义游标cursor1
8 select * from table1 --使用游标的对象(跟据需要填入select文)
9 open cursor1 --打开游标
10
11 fetch next from cursor1 into @id,@name --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中
12
13 while @@fetch_status=0 --判断是否成功获取数据
14 begin
15 update table1 set name=name+'1'
16 where id=@id --进行相应处理(跟据需要填入SQL文)
17
18 fetch next from cursor1 into @id,@name --将游标向下移1行
19 end
20
21 close cursor1 --关闭游标
22 deallocate cursor1
游标一般格式:
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(20)) --待测试的数据表
create table #Temp2(deptid int,username varchar(20)) --结果表
--先把一些待测试的数据插入到待测试表#Temp1中
insert into #Temp1
select 1,100,'a' union all
select 1,101,'b' union all
select 1,131,'d' union all
select 1,201,'f' union all
select 2,302,'c' union all
select 2,202,'a' union all
select 2,221,'e' union all
select 3,102,'y' union all
select 3,302,'e' union all
select 3,121,'t'
--
declare @deptid int,@username varchar(20)
--定义游标
declare Select_cursor cursor for
select deptid,username from #Temp1
open Select_cursor
fetch next from Select_cursor into @deptid,@username --提取操作的列数据放到局部变量中
while @@fetch_status=0 --返回被 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
/*
功能:数据库表格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(20)) --待测试的数据表
create table #Temp2(deptid int,username varchar(20)) --结果表
--先把一些待测试的数据插入到待测试表#Temp1中
insert into #Temp1
select 1,100,'a' union all
select 1,101,'b' union all
select 1,131,'d' union all
select 1,201,'f' union all
select 2,302,'c' union all
select 2,202,'a' union all
select 2,221,'e' union all
select 3,102,'y' union all
select 3,302,'e' union all
select 3,121,'t'
--
declare @deptid int,@username varchar(20)
--定义游标
declare Select_cursor cursor for
select deptid,username from #Temp1
open Select_cursor
fetch next from Select_cursor into @deptid,@username --提取操作的列数据放到局部变量中
while @@fetch_status=0 --返回被 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
0 0
- SQL Cursor 基本用法
- SQL Cursor 基本用法
- SQL Cursor 基本用法
- SQL Cursor 基本用法
- SQL Cursor 基本用法
- SQL Cursor 基本用法
- SQL Cursor 基本用法
- SQL Cursor 基本用法
- SQL Cursor 基本用法
- SQL Cursor(游标) 基本用法
- Sql cursor 基本应用
- SQL(一) cursor用法
- Oracle 游标Cursor 的基本用法
- Oracle 游标Cursor 的基本用法
- PL/SQL Cursor游标的基本使用
- PL/SQL Cursor常见用法模板
- PL/SQL Cursor常见用法模板 .
- Cursor 用法
- Android自定义控件按钮
- Django_CSRF_原理分析
- 如何使用Dreamweaver CS6编写PHP程序,如何使用DW创建PHP站点
- [Raspbian]RaspberryPi重刷Raspbian系统
- Java实现员工管理系统(数据库存储版)
- SQL Cursor 基本用法
- ios 应用实现快速的临时缓存之模型的自动归档
- QT学习记录(一)事件 (event)
- Android下滑条ScrollView
- jdk jre 介绍
- 如何查看url的IP地址,以及IP地址所属区域
- 设计模式之观察者模式
- 项目练习--js操作删除DIV层中的子节点
- 有关spring的知识-----bean的实例化方法