SQL中游标的使用--遍历数据逐行更…
来源:互联网 发布:物业台账软件 编辑:程序博客网 时间:2024/06/08 04:29
--------------------------------------例子1单纯的游标--------------------------------
--
--
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 notnull
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 wherea=@name
-----------------------------------------
fetch next from mycursor into @name
end
close mycursor
deallocate mycursor
go
--------------------------------------例子2存储过程的使用游标------------------------
------------------------------建表----------------------------------
create table #aaa (id varchar(30),name varchar(30),salaryfloat)
go
insert into #aaa values('111','张三',4000)
insert into #aaa values('222','李四',5000)
insert into #aaa values('333','王五',6000)
drop table #aaa
drop table #bbb
create table #bbb (id varchar(30),AddSalary float)
go
insert into #bbb values('111',2000)
insert into #bbb values('222',2000)
insert into #bbb values('333',2000)
select * from #aaa
select * from #bbb
------------------------------建表end----------------------------------
------------------------------建立存储过程和游标----------------------------------
create
as
declare @id varchar(30)
declare @salary float
declare mycursor cursor for selectid,AddSalary
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中取所有人员列表,从表2中取上机次数和管理员.
如果不算管理员那一列的话,我是这样写的。
SELECT
FROM
GROUP BY 表1.名称
create table 表1( --drop table 表1
ID
NAME
QQ
PHONE
)
insert into 表1 values(1
insert into 表1 values(2
insert into 表1 values(3
create table 表2( --drop table 表2
ID
NAME
上机时间
管理员
)
insert into 表2
insert into 表2
insert into 表2
insert into 表2
insert into 表2
go
select * from 表1
select * from 表2
-----------------------------------------------------灵活的函数----------------------------------createfunction GetNameStr(@name nvarchar(10))
returns nvarchar(800)
as
begin
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
group by 表2.name
- SQL中游标的使用--遍历数据逐行更…
- SQL中游标的使用
- SQL中游标的使用
- sql中游标的使用
- SQL中游标的使用
- SQL中游标的使用
- sql 中游标的使用
- SQL中游标的使用
- SQL中游标的使用
- sql中游标的使用
- sql中游标的使用
- SQL中游标的使用
- sql server 中游标的使用……
- 关于SQL中游标的使用
- SQL中游标的使用(转)
- PL/SQL中游标的使用
- PL/SQL中游标的使用
- SQL中游标的使用(转)
- 控件ChartControl的完美示例
- channelArtlist嵌套
- 类型强制转换 And 判断DataRow非空…
- Linux文件或目录的读,写,执行权限
- convertView setOnTouchListener 监听不到 touch up,move等 事件
- SQL中游标的使用--遍历数据逐行更…
- UL标签的padding与margin默认值 40px
- 诡异的Group by函数
- dedearclist 调用文章内容方法
- 4、关于String类的split()方法和append()方法
- SQL 自定义函数实例
- dede list调用文章内容方法
- 寻找春天 九宫格日记-2012.03.09
- Update级联更新