游标代码举例

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航