更改存储过程以使用快照隔离级别

时间:2012-03-27 18:46:01

标签: sql-server database sql-server-2008 tsql sql-server-2005

快速提问我有一个使用

启用快照的数据库
ALTER DATABASE myDB
SET ALLOW_SNAPSHOT_ISOLATION ON

我正在尝试更改现有存储过程以使用事务隔离级别来读取未提交的内容,如下所示:

USE [myDB]
GO
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
GO
/****** Object:  StoredProcedure [dbo].[myStoredProcedure]    Script Date: 03/27/2012 11:39:24 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[myStoredProcedure]

AS
BEGIN
    SELECT *
    FROM someTable
END

RETURN 0

但是当我重新打开存储过程时,SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED语句就消失了。

USE [myDB]
GO
/****** Object:  StoredProcedure [dbo].[myStoredProcedure]    Script Date: 03/27/2012 11:39:24 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[myStoredProcedure]

AS
BEGIN
    SELECT *
    FROM someTable
END

RETURN 0

交易级别是否实际设定?我希望在关闭并重新打开存储过程窗口后,该语句仍然存在。只是想验证,谢谢。

1 个答案:

答案 0 :(得分:1)

你必须把它放在程序体中。如果它在外面,你只是在隔离级别改变它,而不是改变程序定义来使用它。

USE [myDB]
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[myStoredProcedure]

AS
BEGIN
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    SELECT *
    FROM someTable
END