游标简单案例(增删改)

--表create table SP(ID int primary key identity,Name varchar(50), --商品名称Price int, --价格GZ varchar(50) --关注度)--创建存储过程Gocreateproc V_Addasbegin --创建游标 declar...
摘要由CSDN通过智能技术生成

--表

create table SP
(
ID int primary key identity,
Name varchar(50), --商品名称
Price int,  --价格
GZ varchar(50)  --关注度
)

--创建存储过程

Go

create proc V_Add

as

begin

           --创建游标

            declare Adds cursor scroll

             for

             select * from SP

             --打开游标

             open  Adds 

             --查询语句

            declare @Price int
            declare @IDs int

            fetch next from V_O into @Price,@IDs

            if(@Price>100)
            update SP  set Price=price+price*0.1 where ID = @ID
            else if(@Price>50)
            update SP  set Price=price+price*0.05 where ID = @ID
            while (@@FETCH_STATUS = 0)
            begin
            fetch next from V_O into @Price,@IDs
            if(@Price>100)
            update SP  set Price=price+(Price*0.1) where ID = @ID
            else if(@Price>50)
            update SP  set Price=price+(Price*0.05) where ID = @ID
            end
            --关闭游标
            close V_O
            --释放游标
            deallocate V_o

end

--执行游标

exec V_Add

--删除
go
create proc Deletes
(
@Id int
)
as 
begin 
    --创建游标
    declare V_delete cursor scroll
    for
    select ID from UNT1
    --打开游标
    open V_delete    
    --查看数据
    declare @SID int 
    fetch next from V_delete into @SID
    if(@SID = @id)
    delete from UNT1 where ID=@SID
    while (@@FETCH_STATUS = 0)
    begin
    fetch next from V_delete into @SID
    if(@SID = @id)
    delete from UNT1 where ID=@SID    
    end
    --关闭游标
    close V_delete
    --释放游标
    deallocate V_delete
end

--修改
go
alter proc V_change
(
@CName varchar(50),
@China int,
@Math int,
@English int,
@Rema varchar(50),
@ID int
)
as
begin 
    --创建游标
    declare Edi cursor scroll
    for
    select ID,SNAME,CHINA,MATH,ENGLISH,REMA from UNT1
    --打开游标
    open Edi
    --查询数据
    declare @CID int
    declare @Name varchar(50),@Ch int,@sx int,@ey int ,@re varchar(50)    
    fetch next from Edi into @CID,@Name,@Ch,@sx,@ey,@re
    if(@CID = @ID)
    update UNT1 set SNAME=@CName,CHINA=@Ch,MATH=@sx,ENGLISH=@ey,REMA=@re where Id=@CID

    while(@@FETCH_STATUS = 0)
    BEGIN
    fetch next from Edi into @CID,@Name,@Ch,@sx,@ey,@re
    if(@CID = @ID)
    update UNT1 set SNAME=@CName,CHINA=@Ch,MATH=@sx,ENGLISH=@ey,REMA=@re where Id=@CID    
    END
    --关闭游标
    close Edi
    --释放游标
    deallocate Edi
end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值