SQL Server重溫 事務(wù)
更新時間:2012年08月03日 16:50:22 作者:
當對多個表進行更新的時候,某條執(zhí)行失敗。為了保持數(shù)據(jù)的完整性,需要使用事務(wù)回滾
為什么使用事務(wù)
當對多個表進行更新的時候,某條執(zhí)行失敗。為了保持數(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; -- 啟動隱式事務(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)建存儲過程
create procedure qiantaoProc
@asd nchar(10)
as
begin
begin try
begin transaction innerTrans
save transaction savepoint --創(chuàng)建事務(wù)保存點
insert into shiwu (asd) values (@asd);
commit transaction innerTrans
end try
begin catch
rollback transaction savepoint --回滾到保存點
commit transaction innerTrans
end catch
end
go
begin transaction outrans
exec qiantaoProc 'asdasd';
rollback transaction outrans
事務(wù)嵌套,回滾外層事務(wù)時,如果嵌套內(nèi)的事務(wù)已經(jīng)回滾過則會有異常。此時需要使用事務(wù)保存點。如上代碼。
當對多個表進行更新的時候,某條執(zhí)行失敗。為了保持數(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; -- 啟動隱式事務(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)建存儲過程
create procedure qiantaoProc
@asd nchar(10)
as
begin
begin try
begin transaction innerTrans
save transaction savepoint --創(chuàng)建事務(wù)保存點
insert into shiwu (asd) values (@asd);
commit transaction innerTrans
end try
begin catch
rollback transaction savepoint --回滾到保存點
commit transaction innerTrans
end catch
end
go
begin transaction outrans
exec qiantaoProc 'asdasd';
rollback transaction outrans
事務(wù)嵌套,回滾外層事務(wù)時,如果嵌套內(nèi)的事務(wù)已經(jīng)回滾過則會有異常。此時需要使用事務(wù)保存點。如上代碼。
相關(guān)文章
SQL Server2008數(shù)據(jù)庫導入導出兼容性處理方案
SQL Server 的高版本數(shù)據(jù)庫恢復到低版本則可能會有兼容性問題,下面為大家介紹的是如何解決此類問題2014-05-05
SQL Server數(shù)據(jù)庫的高性能優(yōu)化經(jīng)驗總結(jié)
小編以前在做ASP及.NET的時候經(jīng)常用到SQL SERVER,現(xiàn)在用PHP雖然大多數(shù)時候用MYSQL,但不泛有些客戶要在原來SQL的平臺上升級或兼容開發(fā),值得慶幸的是PHP無所不能,基本上所有的數(shù)據(jù)庫它都能連接并支持2011-07-07
SQLSERVER聚集索引和主鍵(Primary Key)的誤區(qū)認識
很多人會把Primary Key和聚集索引搞混起來,或者認為這是同一個東西。這個概念是非常錯誤的,本文將帶你理清思路,感興趣的你可不要錯過了哈,或許本文對你有所幫助2013-02-02
Linux安裝ODBC連接SQLServer數(shù)據(jù)庫的步驟
這篇文章主要介紹了Linux安裝ODBC連接SQLServer數(shù)據(jù)庫?,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
SQLServer 獲得用戶最新或前n條訂單的幾種SQL語句小結(jié)
場景:有一張用戶表,一個訂單表,要求獲得一個用戶對應的最新的一條訂單信息。2011-08-08
sql中的left join及on、where條件關(guān)鍵字的區(qū)別詳解
LEFT JOIN 關(guān)鍵字從左表(table1)返回所有的行,即使右表(table2)中沒有匹配。如果右表中沒有匹配,則結(jié)果為 NULL。這篇文章主要介紹了sql中的left join以及on、where關(guān)鍵字的區(qū)別,需要的朋友可以參考下2018-08-08

