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','100')
    Insert into Table1 values('','asds','80')
    Insert into Table1 values('','asds',null)

    Insert into Table2 values('','90')
    Insert into Table2 values('','100')
    Insert into Table2 values('','80')
    Insert into Table2 values('','95')
    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('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 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   ,'秦云'    ,'10102800'     ,'13500000')

insert into 表1 values(2   ,'在路上'  ,'10378'        ,'13600000')

insert into 表1 values(3   ,'LEO'     ,'10000'        ,'13900000')

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

 

转载于:https://www.cnblogs.com/AnXinliang/p/5198525.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值