SQL中游标的使用--遍历数据逐行更新或删除:相当于for循环
--------------------------------------例子1 单纯的游标--------------------------------
create TABLE Table1
(
a varchar(10),
b varchar(10),
c varchar(10),
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
a ASC
)
) ON [PRIMARY] create TABLE Table2
(
a varchar(10),
c varchar(10),
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
(
a ASC
)
) ON [PRIMARY]
GO Insert into Table1 values('赵','asds',null)
Insert into Table1 values('钱','asds','')
Insert into Table1 values('孙','asds','')
Insert into Table1 values('李','asds',null) Insert into Table2 values('赵','')
Insert into Table2 values('钱','')
Insert into Table2 values('孙','')
Insert into Table2 values('李','')
GO -- drop table Table1
-- drop table Table2 select * from Table1
select * from Table2 declare @name varchar(10)
declare @score varchar(10)
set @name=''
set @score=''
declare mycursor cursor for select a from Table2 where c is not null
open mycursor
fetch next from mycursor into @name
while(@@fetch_status=0)
begin
-----------------------------------------
select @score=c from Table2 where a=@name
update Table1 set c=@score where a=@name
-----------------------------------------
fetch next from mycursor into @name
end
close mycursor
deallocate mycursor
go
--------------------------------------例子2 存储过程的使用游标------------------------
------------------------------建表----------------------------------
create table #aaa (id varchar(30),name varchar(30),salary float)
go
insert into #aaa values('','张三',4000)
insert into #aaa values('','李四',5000)
insert into #aaa values('','王五',6000) drop table #aaa
drop table #bbb create table #bbb (id varchar(30),AddSalary float)
go insert into #bbb values('',2000)
insert into #bbb values('',2000)
insert into #bbb values('',2000) select * from #aaa
select * from #bbb
------------------------------建表end---------------------------------- ------------------------------建立存储过程和游标----------------------------------
create proc PK_test
as
declare @id varchar(30)
declare @salary float declare mycursor cursor for select id,AddSalary from #bbb
open mycursor
fetch next from mycursor into @id,@salary while(@@fetch_status=0)
begin
update #aaa set salary=(salary+@salary) where id=@id
fetch next from mycursor into @id,@salary
end
close mycursor
deallocate mycursor
go exec PK_test drop proc PK_test ------------------------------建立存储过程和游标结束----------------------------------
--------------------------------------例子3 在自定义函数里使用游标--------------------------------功能需求: 问题:
假设环境如下:
表1: ID, NAME, QQ, PHONE,
表中数据: 1 秦云 10102800 13500000
2 在路上 10378 13600000
3 LEO 10000 13900000
表2: ID, NAME, 上机时间,管理员,
表中数据: 1 秦云 2004-1-1 李大伟
2 秦云 2005-1-1 马化腾
3 在路上 2005-1-1 马化腾
4 秦云 2005-1-1 李大伟
5 在路上 2005-1-1 李大伟
实现目的:从表1中取所有人员列表,从表2中取上机次数和管理员.
上机人员名单 上机次数 管理员(上这几次机的每个管理员都列出来)
秦云 3 李大伟,马化腾,李大伟
在路上 2 马化腾,李大伟
LEO 0
如果不算管理员那一列的话,我是这样写的。
SELECT 表1.NAME AS 姓名, COUNT(表2.ID) AS 上机次数
FROM 表1 LEFT OUTER JOIN
表2 ON 表1.NAME = 表2.NAME
GROUP BY 表1.名称
create table 表1( --drop table 表1
ID int,
NAME varchar(10),
QQ varchar(10),
PHONE varchar(20)
)
insert into 表1 values(1 ,'秦云' ,'' ,'')
insert into 表1 values(2 ,'在路上' ,'' ,'')
insert into 表1 values(3 ,'LEO' ,'' ,'')
create table 表2( --drop table 表2
ID int,
NAME varchar(10) ,
上机时间 datetime,
管理员 varchar(10)
)
insert into 表2 values(1,'秦云' ,cast('2004-1-1' as datetime),'李大伟')
insert into 表2 values(2,'秦云' ,cast('2005-1-1' as datetime),'马化腾')
insert into 表2 values (3,'在路上' ,cast('2005-1-1' as datetime),'马化腾')
insert into 表2 values(4,'秦云' ,cast('2005-1-1' as datetime),'李大伟')
insert into 表2 values(5,'在路上' ,cast('2005-1-1' as datetime),'李大伟')
go
select * from 表1
select * from 表2
-----------------------------------------------------灵活的函数----------------------------------create function GetNameStr(@name nvarchar(10))
returns nvarchar(800)
as
begin
declare @nameStr nvarchar(800)
declare @tempStr nvarchar(800)
declare @flag int
declare myCur cursor for ( select 管理员 from 表2 where 表2.NAME = @name )
open myCur
fetch next from myCur into @tempStr
set @flag = 0
while @@fetch_status = 0
begin--while 开始
if @flag = 0 --判断,如果是第一次
begin
set @nameStr = @tempStr
end
else
begin --否则,进行处理
set @nameStr = @nameStr + ',' + @tempStr
end
set @flag = @flag + 1 --循环++
fetch next from myCur into @tempStr
end--while 结束
close myCur
deallocate myCur
return @nameStr
end
--------------------------------------------函数调用------------------------------
select 表2.NAME as 姓名, count(ID) as 上机次数, dbo.GetNameStr(表2.NAME) as 管理员
from 表2
where 表2.NAME in ( select 表1.NAME from 表1 )
group by 表2.NAME
select * from 表1
select * from 表2
,count(ID)
select 表2.NAME,count(id),管理员 from 表2 where 表2.Name in ( select NAME from 表1 )
group by 表2.name
SQL中游标的使用--遍历数据逐行更新或删除:相当于for循环的更多相关文章
- SQL 必知必会·笔记<14>更新和删除数据
1. 更新数据 基本的UPDATE语句,由三部分组成: 要更新的表 列名和它们的新值 确定要更新那些行的过滤条件 更新单列示例: UPDATE Customers SET cust_email = ' ...
- SQL中游标的使用
一般情况下,我们用SELECT这些查询语句时,都是针对的一行记录而言,如果要在查询分析器中对多行记录(即记录集)进行读取操作时,则需要使用到游标或WHILE等循环 游标的类型: 1.静态游标(不检测 ...
- SQL中游标的使用(转)
http://www.cnblogs.com/tianguook/archive/2011/03/09/1977987.html 一般情况下,我们用SELECT这些查询语句时,都是针对的一行记录而言, ...
- 对SQL中游标的认识
游标用于按顺序遍历结果集.但一般情况下,应尽量避免使用游标.原因: 1. 游标违背了关系模型,即按集合来考虑问题的思想: 2. 游标逐行对纪录进行操作,会带来额外的开销,使用游标的解决方案通常比使用集 ...
- SQL从一个表查询数据插入/更新到另一个表
示例一: 从数据库表A中查询出数据插入到数据库表B 从数据库DataBaseA的表TDA中查询出数据插入到数据库DataBaseB的表TDB insert into [DataBaseA].[dbo] ...
- SQL Server 2005/2008遍历所有表更新统计信息
DECLARE UpdateStatisticsTables CURSOR READ_ONLY FOR 02 SELECT sst.name, 03 Schema_name(ss ...
- SQL中游标的用法
游标:是用来对表从上下每行循环取值,将值连接成为字符串.例子:对 pubs 数据库的dbo.titles 表.1.取得表中的总价格:select sum(price) from dbo.titles2 ...
- SQL中游标的使用示例
declare @email_source varchar(MAX); --1.原始发件人字段 declare @key_name varchar(50); --2.我方卷号或客户代码 declare ...
- map在遍历数据的过程中删除数据不出错
// Iterator<Map.Entry<String,Long>> entries = Map.entrySet().iterator(); ...
随机推荐
- IOS7学习之路八(iOS 禁止屏幕旋转的方法)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { retu ...
- JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案
JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案 最近在开发WSS RESTful服务的时候, 碰到了这些个纠 ...
- OpenCascade
Hello World of OpenCascade Hello World of OpenCascade eryar@163.com 摘要Abstract:以一个经典的Hello World程序 ...
- Python:Python学习总结
Python:Python学习总结 背景 PHP的$和->让人输入的手疼(PHP确实非常简洁和强大,适合WEB编程),Ruby的#.@.@@也好不到哪里(OO人员最该学习的一门语言). Pyth ...
- asp.net 上传文件
文件上传实例 公司产品中一直是采用 flash 实现文件上传功能,但用户的需求多了以后遇到了越来越多难以解决的问题,最后试着用硕正提供的freeform.小型页面控件来解决. 硕正文件上传的实现途径有 ...
- lua脚本中字符串分割split
function split( s, c ) for item in string.gmatch( s, "(.-)"..c) do print(item); end end s ...
- [置顶] SQL注入安全分析
(一) 应用环境列表 网络互联设备操作系统 序号 操作系统名称 设备名称 脆弱性 1 IOS_路由器_内部_1 route1 2 IOS_路由器_VPN_1 路由器_VPN_1 3 IOS ...
- Java Concurrency (1)
Memory that can be shared betweenthreads is called shared memory or heap memory. The term variable a ...
- Web前端优化需要注意的点
关键在于:如何提高页面访问速度:如何减少服务器负载和带宽压力: 1. cache:包括数据库表的缓存,浏览器缓存,服务器端缓存(代理服务器缓存,CDN缓存,反向代理服务器缓存),web应用程 ...
- Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(八)
Libvirt 是用c写的一个管理虚拟机及其资源(如网络.存储和外设等)的工具库,它不仅支持KVM/QEMU,它还支持xen,Vmware,OpenVZ和VirtualBox等其他HyperVisor ...