問題描述
如果在 BEGIN
之后我有 SET TRANSACTION ISOLATION LEVEL ...
語句,給定的事務級別是否對存儲過程的整個范圍有效,無論我是否是否使用 BEGIN TRANSACTION
?也就是說,如果我有簡單的 SELECT
語句,根據定義它們是原子的/事務性的,它們的默認事務級別是否會設置為給定的級別?
If right after BEGIN
I have SET TRANSACTION ISOLATION LEVEL ...
statement, will the given transaction level in force for the entire scope of the stored procedure regardless if I use BEGIN TRANSACTION
or not? Namely if I have simple SELECT
statements, which are atomic/transacted by definition, will the default transaction level for them set to the given one?
BEGIN
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
-- will a transaction level for a atomic transaction created by SQL Server for this statement be READ COMMITTED
SELECT * FROM T
END
推薦答案
首先,SQL Server 中的默認隔離級別是 Read Committed,因此除非您更改了默認隔離級別,否則該語句不會真正執行任何操作.
First, the default isolation level in SQL Server is Read Committed, so that statement doesn't really do anything unless you've changed the default isolation level.
但是,一般來說,是的,SET Transaction Isolation Level 會改變整個過程的隔離級別(實際上是連接的持續時間)
But, in general, yes, SET Transaction Isolation Level will change the isolation level for the whole procedure (the duration of the connection, in fact)
請記住,所有 SQL 語句都是隱式事務,這意味著,例如,如果更新失敗 99%,它將自動回滾;不需要 BEGIN TRAN/COMMIT
.
Keep in mind that all SQL statements are implicit transactions meaning that if, for example, an update fails 99% through, it will rollback automatically; no BEGIN TRAN/COMMIT
is necessary.
要回答您的問題,是的,您的 SELECT 語句將繼承您設置的隔離級別(如果未設置,則為默認值),除非您使用諸如 WITH NOLOCK
之類的查詢提示覆蓋該行為這將使單個查詢的行為就像您執行了 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
To answer your question, yes, your SELECT statements will inherit the isolation level you set (or the default if you do not set one) unless you override the behavior with a query hint like WITH NOLOCK
which will make the individual query behave as though you did SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
這篇關于SQL Server:如何為整個存儲過程設置默認隔離級別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!