开发者

Why context_info can fail?

开发者 https://www.devze.com 2023-01-07 04:10 出处:网络
I am using context_info to simulate a global variable, I use it in this context. I have scripts made up of several batches, I set a global variable at the beginning (123 in the example开发者_Go百科)

I am using context_info to simulate a global variable, I use it in this context.

I have scripts made up of several batches, I set a global variable at the beginning (123 in the example开发者_Go百科) and then after all the batches I use that number to populate a log table.

Now this works, but today it failed on one machine, I cannot figure why. Is there a reason why context_info should not work? I tried to manually replace context_info with 123 and the script was succesful. The script was run from a Delphi application.

DECLARE @GlobalInteger binary(128)
SET @GlobalInteger =cast(123 as binary(128))
SET CONTEXT_INFO @GlobalInteger 
GO

BATCH 1
GO

...

BATCH N
GO

INSERT INTO TABLE_LOG VALUES (cast(context_info() as INT)


There are some Remarks in the CONTEXT_INFO documentation:

Multiple active result sets (MARS) enables applications to run multiple batches, or requests, at the same time on the same connection. When one of the batches on a MARS connection runs SET CONTEXT_INFO, the new context value is returned by the CONTEXT_INFO function when it is run in the same batch as the SET statement. The new value is not returned by the CONTEXT_INFO function run in one or more of the other batches on the connection, unless they started after the batch that ran the SET statement completed

There are also some explanations on how the value propagates to the various views exposing it (sys.dm_exec_requests, sys.dm_exec_sessions and CONTEXT_INFO itself), see Using Session Context Information. In some places the information is visible right away, in other places is visible only after the batch has completed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号