游标代码举例

1.对低于当前平均价格的书,均提价50%

2对于高于或等于当前平均价格的书,均降价25%

代码如下:

declare title_update cursor

     for select title_id,price from titles

     for update

go

局部变量

declare @avg_price money,@title_id tid,@price money

open title_update

begin tran

计算平均书价

select @avg_price=avg(price) from titles holdlock

fetch title_update into @title_id,@price

while @@sqlstatus!=2

begin

if  @@sqlstatus=1

begin

rollback tran

raiserror 21001 "Fetch failed in cursor"

close title_update

deallocate cursor title_update

return

end

if @price<@avg_price

提价50%

update titles set price = price * $1.50

where current of title_update

else

降价25%

update titles set price = price * $.75

where current of title_update

if @@error!=0

begin

rollback tran

raiserror 22001 "Update failed"

close title_update

deallocate cursor title_update

return

end

fetch title_update into @title_id,@price

end

commit

close title_update

deallocate cursor title_update

go

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值