SQL Server 2005 事务级和存储过程

SQL Server 2005 Transaction Level and Stored Procedures(SQL Server 2005 事务级和存储过程)
本文介绍了SQL Server 2005 事务级和存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如果我使用命令 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 然后在同一上下文中使用 EXEC storedProcedureName 执行存储过程,该存储过程是否会使用该事务之前声明的级别还是将使用默认级别?

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?

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

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.:该系统建立在 .NET 2.0 和专有第三方产品之上,但存在局限性,因此需要这些变通方法.

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

推荐答案

存储过程将使用调用时生效的事务隔离.

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.

(刚刚检查过,这是与 BOL 所说的相反...它保持为该连接设置,直到它被明确更改"但可以从下面看到)

( 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

结果

ReadUncomitted
Enter: SetRCTransLevel
Readcomitted
Exit: SetRCTransLevel
ReadUncomitted

这篇关于SQL Server 2005 事务级和存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to register System.DirectoryServices for use in SQL CLR User Functions?(如何注册 System.DirectoryServices 以在 SQL CLR 用户函数中使用?)
Query From LDAP for User Groups(从 LDAP 查询用户组)
How can I get DOMAINUSER from an AD DirectoryEntry?(如何从 AD DirectoryEntry 获取 DOMAINUSER?)
Get a list of members of a WinNT group(获取 WinNT 组的成员列表)
Get UPN or email for logged in user in a .NET web application(在 .NET Web 应用程序中获取登录用户的 UPN 或电子邮件)
Active Directory Group Membership Checking in .Net 4.5(.Net 4.5 中的 Active Directory 组成员身份检查)