日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

簡單例子理解數(shù)據(jù)庫事務(wù)

系統(tǒng) 2118 0


簡單例子理解數(shù)據(jù)庫事務(wù)

?

    /*-- 創(chuàng)建表 --*/

--創(chuàng)建農(nóng)行賬戶表bank

if exists(select * from sysobjects where name='bank')

	drop table bank

go

create table bank

(

	customerName char(10),		--顧客姓名

	currentMoney money			--當(dāng)前余額

)

/*-- 添加約束:根據(jù)銀行規(guī)定,賬戶余額不能少于1元,否則視為銷戶 --*/

alter table bank

add constraint CK_currentMoney check(currentMoney>=1)

/*-- 插入測試數(shù)據(jù):張三開戶,開戶金額為800,李四開戶,開戶金額1 --*/

insert into bank(customerName,currentMoney) values('張三',1000)

insert into bank(customerName,currentMoney) values('李四',1)

--查看結(jié)果

select * from bank

go



/*-- 轉(zhuǎn)賬測試:張三希望通過轉(zhuǎn)賬,直接匯錢給李四1000元 --*/

set nocount on --不顯示受影響的行數(shù)信息

print '查看轉(zhuǎn)賬事務(wù)前的余額'

select * from bank

go

/*-- 開始事務(wù) --*/

begin transaction

/*-- 定義變量,用于累計(jì)事務(wù)執(zhí)行的過程中的錯(cuò)誤 --*/

declare @errorSum int

set @errorSum=0		--初始化為0,即無錯(cuò)誤

/*-- 轉(zhuǎn)賬 --*/

update bank set currentMoney=currentMoney-1000 where customerName='張三'

set @errorSum=@errorSum+@@error		--累計(jì)是否有錯(cuò)誤

update bank set currentMoney=currentMoney+1000 where customerName='李四'

set @errorSum=@errorSum+@@error		--累計(jì)是否有錯(cuò)誤



print '查看轉(zhuǎn)賬事務(wù)過程中的余額'

select * from bank



/*-- 根據(jù)是否有錯(cuò)誤,確定事務(wù)是提交還是撤銷 --*/

if @errorSum<>0		--如果有錯(cuò)誤

	begin

		print '交易失敗,回滾事務(wù)'

		rollback transaction

	end

else

	begin

		print '交易成功,提交事務(wù),寫入硬盤,永久保存'

		commit transaction

	end



print '查看轉(zhuǎn)賬事務(wù)后的余額'

select * from bank

go
  

簡單例子理解數(shù)據(jù)庫事務(wù)

?

?

簡單例子理解數(shù)據(jù)庫事務(wù)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 原平市| 历史| 资溪县| 兴宁市| 诸暨市| 博白县| 凤翔县| 普兰店市| 勃利县| 平谷区| 万源市| 云霄县| 霞浦县| 龙里县| 兰西县| 咸阳市| 冀州市| 周口市| 本溪市| 台南市| 定南县| 恩平市| 云龙县| 驻马店市| 鄯善县| 黄山市| 汝阳县| 吉隆县| 共和县| 孙吴县| 二手房| 天峻县| 云林县| 商都县| 隆化县| 黔南| 普格县| 隆子县| 保康县| 乡宁县| 南投市|