存储过程中指定的隔离级别是否继续执行该存储过程中的动态查询?
CREATE PROCEDURE MySP AS
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
DECLARE @dSQL VARCHAR(max) = 'SELECT col FROM table';
EXEC(@dSQL);
END
那么,动态查询中的隔离级别是什么?
答案 0 :(得分:3)
它确实贯穿于动态查询,您可以查看以下内容:
DBCC useroptions;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
DECLARE @dSQL VARCHAR(max) = 'DBCC useroptions';
EXEC(@dSQL);
答案 1 :(得分:1)
我相信你确实带有隔离级别。我做了这个测试,并且在动态查询
上获得了隔离级别1-- Create the SP (my test is on AdventureWorks2012)
CREATE PROCEDURE MySP AS
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
DECLARE @dSQL VARCHAR(max) = 'SELECT * FROM Person.Person; WAITFOR DELAY ''02:00'';';
EXEC(@dSQL);
END
-- Exec SP
EXEC MySP
-- Check the isolation level while your dynamic code is running
SELECT transaction_isolation_level, * FROM sys.sysprocesses t1
JOIN sys.dm_exec_requests t2 on t1.spid = t2.session_id
WHERE t1.spid > 50
答案 2 :(得分:0)
比TRANSACTION ISOLATION LEVEL
更普遍,SET
语句是会话级别,因此继续执行子流程。但是,虽然可以在子进程中更改它们,但这些更改不会返回到调用/父上下文。
有关详细信息,请参阅SET Statements的MSDN页面。
您可以通过执行以下操作轻松测试:
SSMS查询标签1:
-- DROP TABLE ##tmp;
CREATE TABLE ##tmp (Col1 INT);
INSERT INTO ##tmp (Col1) VALUES (1);
BEGIN TRAN
SELECT * FROM ##tmp WITH (HOLDLOCK, TABLOCKX);
WAITFOR DELAY '00:02:00.000'; -- 2 minute timer
ROLLBACK TRAN;
SSMS查询标签2:
-- First, highlight the following and run. It will hang. Cancel the query.
-- SELECT * FROM ##tmp;
-- Second, hit F5. It will return the row twice.
-- If the SET command in the EXEC affected the parent process, the 2nd SELECT
-- would hang.
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
EXEC (N' SELECT * FROM ##tmp; SET TRANSACTION ISOLATION LEVEL READ COMMITTED;');
SELECT * FROM ##tmp;