SQL Server 2005 事务级别和存储过程

发布于 2024-12-06 08:57:09 字数 324 浏览 0 评论 0原文

如果我使用命令SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED,然后在同一上下文中使用EXECstoredProcedureName执行存储过程,该存储过程是否会使用前面所述的事务级别或者将使用默认的?

如果我想强制每个存储过程在事务级别使用,是否必须在代码顶部包含相同的语句(SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED)?

PS:该系统构建在 .NET 2.0 和专有第三方产品之上,存在局限性,因此需要这些解决方法。

If I use the command SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED and then execute a stored procedure using the EXEC storedProcedureName on the same context, will the stored procedure use the transaction level stated previously or will use a default one?

If I want to force every stored procedure to use on transaction level do I have to include at the top of the code the same statement (SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED)?

PS.: the system is built on top of .NET 2.0 and proprietary third party products with limitations, hence the need of these workarounds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2024-12-13 08:57:09

存储过程将使用调用时有效的事务隔离。

如果存储过程本身设置了显式隔离级别,则该隔离级别将在存储过程退出时重置。

(编辑:刚刚检查过,这与 BOL 所说的相反“...它仍然设置对于该连接,直到它被显式更改”,但可以从下面看到)

CREATE PROC CheckTransLevel
AS
DECLARE @Result varchar(20)

SELECT @Result = CASE transaction_isolation_level 
                        WHEN 0 THEN 'Unspecified' 
                        WHEN 1 THEN 'ReadUncomitted' 
                        WHEN 2 THEN 'Readcomitted' 
                        WHEN 3 THEN 'Repeatable' 
                        WHEN 4 THEN 'Serializable' 
                        WHEN 5 THEN 'Snapshot' 
                  END 
FROM sys.dm_exec_sessions 
WHERE session_id = @@SPID

PRINT @Result

GO
CREATE PROC SetRCTransLevel
AS
PRINT 'Enter: SetRCTransLevel'
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
EXEC CheckTransLevel
PRINT 'Exit: SetRCTransLevel'
GO

SET NOCOUNT ON

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

EXEC CheckTransLevel

EXEC SetRCTransLevel

EXEC CheckTransLevel

结果

ReadUncomitted
Enter: SetRCTransLevel
Readcomitted
Exit: SetRCTransLevel
ReadUncomitted

The stored procedure will use the transaction isolation in effect when called.

If the stored procedure itself sets an explicit isolation level this will be reset when the stored procedure exits.

(Edit: Just checked and this is contrary to what BOL says "... it remains set for that connection until it is explicitly changed" but can be seen from the below)

CREATE PROC CheckTransLevel
AS
DECLARE @Result varchar(20)

SELECT @Result = CASE transaction_isolation_level 
                        WHEN 0 THEN 'Unspecified' 
                        WHEN 1 THEN 'ReadUncomitted' 
                        WHEN 2 THEN 'Readcomitted' 
                        WHEN 3 THEN 'Repeatable' 
                        WHEN 4 THEN 'Serializable' 
                        WHEN 5 THEN 'Snapshot' 
                  END 
FROM sys.dm_exec_sessions 
WHERE session_id = @@SPID

PRINT @Result

GO
CREATE PROC SetRCTransLevel
AS
PRINT 'Enter: SetRCTransLevel'
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
EXEC CheckTransLevel
PRINT 'Exit: SetRCTransLevel'
GO

SET NOCOUNT ON

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

EXEC CheckTransLevel

EXEC SetRCTransLevel

EXEC CheckTransLevel

Results

ReadUncomitted
Enter: SetRCTransLevel
Readcomitted
Exit: SetRCTransLevel
ReadUncomitted
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文