問(wèn)題描述
如果在 BEGIN
之后我有 SET TRANSACTION ISOLATION LEVEL ...
語(yǔ)句,給定的事務(wù)級(jí)別是否對(duì)存儲(chǔ)過(guò)程的整個(gè)范圍有效,無(wú)論我是否是否使用 BEGIN TRANSACTION
?也就是說(shuō),如果我有簡(jiǎn)單的 SELECT
語(yǔ)句,根據(jù)定義它們是原子的/事務(wù)性的,它們的默認(rèn)事務(wù)級(jí)別是否會(huì)設(shè)置為給定的級(jí)別?
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 中的默認(rèn)隔離級(jí)別是 Read Committed,因此除非您更改了默認(rèn)隔離級(jí)別,否則該語(yǔ)句不會(huì)真正執(zhí)行任何操作.
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.
但是,一般來(lái)說(shuō),是的,SET Transaction Isolation Level 會(huì)改變整個(gè)過(guò)程的隔離級(jí)別(實(shí)際上是連接的持續(xù)時(shí)間)
But, in general, yes, SET Transaction Isolation Level will change the isolation level for the whole procedure (the duration of the connection, in fact)
請(qǐng)記住,所有 SQL 語(yǔ)句都是隱式事務(wù),這意味著,例如,如果更新失敗 99%,它將自動(dòng)回滾;不需要 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.
要回答您的問(wèn)題,是的,您的 SELECT 語(yǔ)句將繼承您設(shè)置的隔離級(jí)別(如果未設(shè)置,則為默認(rèn)值),除非您使用諸如 WITH NOLOCK
之類的查詢提示覆蓋該行為這將使單個(gè)查詢的行為就像您執(zhí)行了 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
這篇關(guān)于SQL Server:如何為整個(gè)存儲(chǔ)過(guò)程設(shè)置默認(rèn)隔離級(jí)別?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!