事務定義(SQL SERVER 2008)

一、 實驗題目:事務定義
二、 實驗目的:掌握事務的定義、提交、回滾等命令,通過這些命令的使用進一步理解事務的概念和性質。
題目六:
藥品(編號,名稱,價格,廠商)
處方(藥品編號,數量,醫生編號)
醫生 (編號,姓名,科室,職稱)
定義一個事務,完成下列步驟:
1統計藥品平均價格,若平均價格小於20,給每種藥增加一元,直至其平均價格超過20(每增一次顯示一次)。
2顯示所有藥品名稱 、價格。
3 若有藥品超過50元,取消上述操作。
4提交後顯示藥品價格。

定義事務:

begin transaction ManageMedical

declare @avg_price decimal(18,2);
select @avg_price = convert(decimal(18,2),AVG(價格)) from 藥品;
print'初始平均價格爲:'
print @avg_price

while @avg_price < 20
begin
update 
藥品 set 價格 = 價格 + 1
select @avg_price = convert(decimal(18,2),AVG(價格)) from 藥品;
print'當前平均價格爲:'
print @avg_price
end

select 名稱,價格 from 藥品

if exists(select * from 藥品 where 價格 > 50)
begin
rollback transaction ManageMedical;
print '有藥品價格超過50元,事務已回滾'
end
else
begin
commit transaction ManageMedical;
print '沒有藥品價格超過50元,事務已提交'
end

select 名稱,價格 from 藥品;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章