SQL Server重溫 事務(wù)
更新時(shí)間:2012年08月03日 16:50:22 作者:
當(dāng)對(duì)多個(gè)表進(jìn)行更新的時(shí)候,某條執(zhí)行失敗。為了保持?jǐn)?shù)據(jù)的完整性,需要使用事務(wù)回滾
為什么使用事務(wù)
當(dāng)對(duì)多個(gè)表進(jìn)行更新的時(shí)候,某條執(zhí)行失敗。為了保持?jǐn)?shù)據(jù)的完整性,需要使用事務(wù)回滾。
顯示設(shè)置事務(wù)
begin try
begin transaction
insert into shiwu (asd) values ('aasdasda');
commit transaction
end try
begin catch
select ERROR_NUMBER() as errornumber
rollback transaction
end catch
隱式設(shè)置事務(wù)
set implicit_transactions on; -- 啟動(dòng)隱式事務(wù)
go
begin try
insert into shiwu (asd) values ('aasdasda');
insert into shiwu (asd) values ('aasdasda');
commit transaction;
end try
begin catch
select ERROR_NUMBER() as errornumber
rollback transaction; --回滾事務(wù)
end catch
set implicit_transactions off; --關(guān)閉隱式事務(wù)
go
顯示事務(wù)以下語句不能使用,隱式事務(wù)可以
alter database;
backup;
create database;
drop database;
reconfigure;
restore;
update statistics;
顯示事務(wù)可以嵌套使用
--創(chuàng)建存儲(chǔ)過程
create procedure qiantaoProc
@asd nchar(10)
as
begin
begin try
begin transaction innerTrans
save transaction savepoint --創(chuàng)建事務(wù)保存點(diǎn)
insert into shiwu (asd) values (@asd);
commit transaction innerTrans
end try
begin catch
rollback transaction savepoint --回滾到保存點(diǎn)
commit transaction innerTrans
end catch
end
go
begin transaction outrans
exec qiantaoProc 'asdasd';
rollback transaction outrans
事務(wù)嵌套,回滾外層事務(wù)時(shí),如果嵌套內(nèi)的事務(wù)已經(jīng)回滾過則會(huì)有異常。此時(shí)需要使用事務(wù)保存點(diǎn)。如上代碼。
當(dāng)對(duì)多個(gè)表進(jìn)行更新的時(shí)候,某條執(zhí)行失敗。為了保持?jǐn)?shù)據(jù)的完整性,需要使用事務(wù)回滾。
顯示設(shè)置事務(wù)
復(fù)制代碼 代碼如下:
begin try
begin transaction
insert into shiwu (asd) values ('aasdasda');
commit transaction
end try
begin catch
select ERROR_NUMBER() as errornumber
rollback transaction
end catch
隱式設(shè)置事務(wù)
復(fù)制代碼 代碼如下:
set implicit_transactions on; -- 啟動(dòng)隱式事務(wù)
go
begin try
insert into shiwu (asd) values ('aasdasda');
insert into shiwu (asd) values ('aasdasda');
commit transaction;
end try
begin catch
select ERROR_NUMBER() as errornumber
rollback transaction; --回滾事務(wù)
end catch
set implicit_transactions off; --關(guān)閉隱式事務(wù)
go
顯示事務(wù)以下語句不能使用,隱式事務(wù)可以
復(fù)制代碼 代碼如下:
alter database;
backup;
create database;
drop database;
reconfigure;
restore;
update statistics;
顯示事務(wù)可以嵌套使用
復(fù)制代碼 代碼如下:
--創(chuàng)建存儲(chǔ)過程
create procedure qiantaoProc
@asd nchar(10)
as
begin
begin try
begin transaction innerTrans
save transaction savepoint --創(chuàng)建事務(wù)保存點(diǎn)
insert into shiwu (asd) values (@asd);
commit transaction innerTrans
end try
begin catch
rollback transaction savepoint --回滾到保存點(diǎn)
commit transaction innerTrans
end catch
end
go
begin transaction outrans
exec qiantaoProc 'asdasd';
rollback transaction outrans
事務(wù)嵌套,回滾外層事務(wù)時(shí),如果嵌套內(nèi)的事務(wù)已經(jīng)回滾過則會(huì)有異常。此時(shí)需要使用事務(wù)保存點(diǎn)。如上代碼。
相關(guān)文章
SQLServer2019配置端口號(hào)的實(shí)現(xiàn)
這篇文章主要介紹了SQLServer2019配置端口號(hào)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04SQl Function 創(chuàng)建函數(shù)實(shí)例介紹
這篇文章主要介紹了SQl Function 創(chuàng)建函數(shù)實(shí)例介紹,需要的朋友可以參考下2016-10-10MS SQL Server數(shù)據(jù)庫清理錯(cuò)誤日志的方法
SQL服務(wù)器磁盤空間爆滿導(dǎo)致數(shù)據(jù)庫無法訪問。遠(yuǎn)程到服務(wù)器上,發(fā)現(xiàn)原來是SQL錯(cuò)誤日志文件惹的禍,數(shù)據(jù)庫在1秒內(nèi)產(chǎn)生上100M大小的日志,沒多長(zhǎng)時(shí)間就將磁盤空間堵滿了,下面說說解決方案2013-11-11sql語句優(yōu)化之用EXISTS替代IN、用NOT EXISTS替代NOT IN的語句
sql語句優(yōu)化之用EXISTS替代IN、用NOT EXISTS替代NOT IN的語句...2007-08-08SQL?Server解析/操作Json格式字段數(shù)據(jù)的方法實(shí)例
SQL SERVER沒有自帶的解析json函數(shù),需要自建一個(gè)函數(shù)(表值函數(shù)),下面這篇文章主要給大家介紹了關(guān)于SQL?Server解析/操作Json格式字段數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-08-08SQL數(shù)據(jù)庫連接超時(shí)時(shí)間已到的問題
這篇文章主要介紹了SQL數(shù)據(jù)庫連接超時(shí)時(shí)間已到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04SQL Server數(shù)據(jù)庫的修復(fù)SQL語句
使用數(shù)據(jù)庫的過程中,由于斷電或其他原因,有可能導(dǎo)致數(shù)據(jù)庫出現(xiàn)一些小錯(cuò)誤,比如檢索某些表特別慢,查詢不到符合條件的數(shù)據(jù)等。2008-11-11