触发器是否可以找到修改数据的存储过程的名称?

发布于 2024-10-04 01:34:17 字数 239 浏览 1 评论 0原文

有一些存储过程通常会被几个不同的系统调用,以对数据库中的一些表进行维护。有些是自动化的,有些不是。

其中一个表有一列的数字有时会关闭,我们不确定何时或为何会发生这种情况。我想在桌子上放置一个触发器,以便我可以看到正在更改的内容以及何时更改,但了解哪个过程启动了修改也很有帮助。

是否可以从触发器中获取存储过程的名称?如果没有,是否有其他方法可以判断导致某些内容被修改的原因? (我也不是在谈论用户,用户的名称在这种情况下没有帮助)。

There are a few stored procedures that routinely get called by a few different systems to do maintenance on a few tables in our database. Some are automated, some aren't.

One of the tables has a column where the number is sometimes off, and we don't know for sure when or why this is happening. I want to put a trigger on the table so I can see what is being changed and when, but it'd also be helpful to know which procedure initiated the modification.

Is it possible to get the name of the stored procedure from the trigger? If not, is there any other way to tell what caused something to be modified? (I'm not talking about the user either, the name of the user doesn't help in this case).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风透绣罗衣 2024-10-11 01:34:17

您可以尝试:CONTEXT_INFO

这里是一个 CONTEXT_INFO 用法示例:

在您想要跟踪的执行插入/删除/更新的每个过程中,添加以下内容:

DECLARE @string        varchar(128)
       ,@CONTEXT_INFO  varbinary(128)
SET @string=ISNULL(OBJECT_NAME(@@PROCID),'none')
SET @CONTEXT_INFO =cast('Procedure='+@string+REPLICATE(' ',128) as varbinary(128))
SET CONTEXT_INFO @CONTEXT_INFO

--do insert/delete/update that will fire the trigger

SET CONTEXT_INFO 0x0 --clears out the CONTEXT_INFO value

这是触发器检索值的部分:

DECLARE @string         varchar(128)
       ,@sCONTEXT_INFO  varchar(128)
SELECT @sCONTEXT_INFO=CAST(CONTEXT_INFO() AS VARCHAR) FROM master.dbo.SYSPROCESSES WHERE SPID=@@SPID

IF LEFT(@sCONTEXT_INFO,9)='Procedure'
BEGIN
    SET @string=RIGHT(RTRIM(@sCONTEXT_INFO),LEN(RTRIM(@sCONTEXT_INFO))-10)
END
ELSE
BEGIN --optional failure code
    RAISERROR('string was not specified',16,1)
    ROLLBACK TRAN
    RETURN
END

..use the @string

you can try: CONTEXT_INFO

here is a CONTEXT_INFO usage example:

in every procedure doing the insert/delete/update that you want to track, add this:

DECLARE @string        varchar(128)
       ,@CONTEXT_INFO  varbinary(128)
SET @string=ISNULL(OBJECT_NAME(@@PROCID),'none')
SET @CONTEXT_INFO =cast('Procedure='+@string+REPLICATE(' ',128) as varbinary(128))
SET CONTEXT_INFO @CONTEXT_INFO

--do insert/delete/update that will fire the trigger

SET CONTEXT_INFO 0x0 --clears out the CONTEXT_INFO value

here is the portion of the trigger to retrieve the value:

DECLARE @string         varchar(128)
       ,@sCONTEXT_INFO  varchar(128)
SELECT @sCONTEXT_INFO=CAST(CONTEXT_INFO() AS VARCHAR) FROM master.dbo.SYSPROCESSES WHERE SPID=@@SPID

IF LEFT(@sCONTEXT_INFO,9)='Procedure'
BEGIN
    SET @string=RIGHT(RTRIM(@sCONTEXT_INFO),LEN(RTRIM(@sCONTEXT_INFO))-10)
END
ELSE
BEGIN --optional failure code
    RAISERROR('string was not specified',16,1)
    ROLLBACK TRAN
    RETURN
END

..use the @string
枕花眠 2024-10-11 01:34:17

我们的系统已经将 CONTEXT_INFO 变量用于其他目的,因此不可用。我还尝试了 DBCC INPUTBUFFER 解决方案 几乎有效。输入缓冲区的缺点是它仅返回外部调用过程。例如:procA 调用 procB 来触发触发器。触发器运行 DBCC INPUTBUFFER,它仅显示 procA。由于我的触发器正在寻找 procB,因此这种方法失败了。

与此同时,我所做的是创建一个临时表。现在procA调用procB。 procB 在临时表中插入一行,然后触发触发器。触发器检查暂存表并找到 procB 条目。返回后,procB 从暂存表中删除其条目。这是一个骗局,但它确实有效。我对这方面的任何反馈都很感兴趣。

Our system is already using the CONTEXT_INFO variable for another purpose so that is not available. I also tried the DBCC INPUTBUFFER solution which almost worked. The draw back to the inputbuffer is that it returns only the outside calling procedure. Ex: procA calls procB which fires a trigger. The trigger runs DBCC INPUTBUFFER which only shows procA. Since my trigger was looking for procB, this approach failed.

What I have done in the meantime is to create a staging table. Now procA calls procB. procB inserts a line in the staging table then fires the trigger. The trigger checks the staging table and finds the procB entry. Upon return procB deletes its entry from the staging table. It's a shell game but it works. I would be interested in any feedback on this.

数理化全能战士 2024-10-11 01:34:17

我没有尝试过这个,但 @@PROCID 看起来它可能会返回你想要的。

I've not tried this but @@PROCID looks like it might return what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文