SqlServer游标的学习

本文深入讲解了SQL Server中游标的基本概念、操作步骤及其应用场景,包括如何声明、打开、读取、关闭和释放游标,并提供了丰富的实战示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-------------------------------------------------------------------------------------
------------------------------------游标的实例---------------------------------------
-------------------------------------------------------------------------------------
--游标的概念
--游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集。使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式。用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合。游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录。
--游标的优点
--从游标定义可以得到游标的如下优点,这些优点使游标在实际应用中发挥了重要作用:
--1)允许程序对由查询语句select返回的行集合中的每一行执行相同或不同的操作,而不是对整个行集合执行同一个操作。
--2)提供对基于游标位置的表中的行进行删除和更新的能力。
--3)游标实际上作为面向集合的数据库管理系统(RDBMS)和面向行的程序设计之间的桥梁,使这两种处理方式通过游标沟通起来。

--FORWARD_ONLY意味着游标只能从数据集开始向数据集结束的方向读取,FETCH NEXT是唯一的选项
--SCROLL支持游标在定义的数据集中向任何方向,或任何位置移动

--游标的操作步骤:
--1.声明游标
--2.打开游标
--3.读取数据
--4.关闭游标
--5.释放游标


declare @name varchar(50)
declare st_cursor scroll cursor for select empName from Tab_Salary   --声明游标
open st_cursor	    --打开游标

					--读取数据
--取第一行
fetch first from st_cursor into @name 
print @name

--取最后一行
fetch last from st_cursor into @name
print @name

--取下一行
fetch next from st_cursor into @name
print @name

--取上一行
fetch prior from st_cursor into @name
print @name

--取第二行【absolute(n):跳到第n行】
fetch absolute 2 from st_cursor into @name
print @name

--取上一行【relative(n):相对当前位置取第n行,-1当前位置的上一行,1当前位置下一行,如果超出n行,则为当前行数据】
fetch relative -1 from st_cursor into @name
print @name

close st_cursor		--关闭游标
deallocate st_cursor --释放游标


------------------------------------实战练习-----------------------------------------
--我们先建两张表
--工资表
create table Tab_Salary
(
	id int identity(1,1),
	empId varchar(20) primary key,	
	empName varchar(50),
	Salary decimal(18,2)
)

insert into Tab_Salary values('A001','陈飞刚',1000.50),
						 ('B001','杨帆',900.50),
						 ('C001','吴磊',750.80)							
go

--额外工资表
create table Tab_Extra_Salary
(
	id int identity(1,1),
	empId varchar(20),
	Extr_Salary decimal(18,2)
)
insert into Tab_Extra_Salary values('A001',200.00),
						 ('B001',100.50),
						 ('C001',600.80),
						 ('B001',800.00)							
go

--现在我们做一个简单的练习:用游标打印出额外工资表Extra_Salary的数据
create proc pro_cursor
as
declare @empId varchar(20)
declare @Extr_Salary decimal(18,2)
declare mycursor cursor for select empId,Extr_Salary from Tab_Extra_Salary --声明游标
open mycursor  --打开游标
fetch next from mycursor into @empId,@Extr_Salary
while(@@FETCH_STATUS=0)  --遍历所有的数据
begin
	print '游标成功取出一条数据!'
	print @empId
	print @Extr_Salary
	
	fetch next from mycursor into @empId,@Extr_Salary  --取下一条游标数据
end
close mycursor --关闭游标
deallocate mycursor --删除游标
go

exec pro_cursor
go

--加强练习:将工资表中员工的工资与该员工额外工资表中的额外工资对应的计算总和,然后在工资表中Salary显示出来
--说简单点:Salary=Salary+Extr_Salary
create proc pro_Salary
as
declare @empId varchar(20)
declare @Extr_Salary decimal(18,2)
declare extrSalary_cursor cursor for select empId,Extr_Salary from Tab_Extra_Salary 
open extrSalary_cursor
fetch next from extrSalary_cursor into @empId,@Extr_Salary
while(@@FETCH_STATUS=0)
begin
	update Tab_Salary set Salary=Salary+@Extr_Salary where empId=@empId
	fetch next from extrSalary_cursor into @empId,@Extr_Salary 
end
close extrSalary_cursor
deallocate extrSalary_cursor
go

exec pro_Salary
go
--来看看刚刚游标执行的效果有没有加上去
select * from Tab_Salary
go

 

 

相关资料:http://www.cnblogs.com/moss_tan_jun/archive/2011/11/26/2263988.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值