我正在使用 context_info 来模拟一个全局变量,我在这个上下文中使用它。
我有由几个批处理组成的脚本,我在开头设置了一个全局变量(示例中为 123
),然后在所有批处理之后我使用该数字填充日志表。
现在这可行,但今天它在一台机器上失败了,我不知道为什么。 context_info 不起作用有什么原因吗?我尝试手动替换context_info
与 123
脚本成功了。该脚本是从 Delphi 应用程序运行的。
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)
最佳答案
CONTEXT_INFO
中有一些备注文档:
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
还有一些关于值如何传播到暴露它的各种 View (sys.dm_exec_requests、sys.dm_exec_sessions 和 CONTEXT_INFO 本身)的解释,请参阅使用 Session Context Information .在某些地方,信息立即可见,而在其他地方,只有在批处理完成后才能看到。
关于sql-server - 为什么 context_info 会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3238073/