使用 DBCC INPUTBUFFER 可以完全捕获参数化查询吗?

发布于 2024-08-17 19:41:42 字数 716 浏览 3 评论 0原文

在 SQL Server 2008 中,我使用触发器来捕获对数据库中特定表所做的所有更改。我的目标是捕捉整个变化。也就是说,捕获正在插入的数据,而不仅仅是正在插入的数据。在触发器中,我使用 DBCC INPUTBUFFER 返回的结果集的 EventInfo 列来获取当前正在执行的 SQL 语句,并使用Parameters 列来获取所使用参数的计数。这在大多数情况下都有效,但是当外部应用程序使用 ADO.NET 执行查询,或者使用 SSMS 中的“编辑前 200 行”插入/删除行时,EventInfo 没有参数值。

例如,如果在查询窗口中执行字符串查询或在 ADO.NET 中作为非参数化字符串查询执行,EventInfo 将显示:

  INSERT INTO DTS_TABLE ([ID] ,[DTS_ID] ,[DTS] ,[TICS])  VALUES (10, 9, GETDATE(), 91234)  

使用“编辑前 200 行”或 ADO.NET 中的参数化查询插入相同数据时,EventInfo 将显示(为了可读性而添加的换行符):

(@id int,@dtsid int,@dts datetime,@tics int)
INSERT INTO DTS_TABLE ([ID] ,[DTS_ID] ,[DTS] ,[TICS])  
VALUES (@id, @dtsid, @dts, @tics)

是否可以在触发器的上下文中访问参数值?

In SQL Server 2008, I am using triggers to capture all changes made to a specific table in my database. My goal is to capture the entire change. That is, to capture what data is being inserted, not just that data is being inserted. In the trigger I am using the EventInfo column of the result set returned by DBCC INPUTBUFFER to get the currently executing SQL statement as well as the Parameters column to get a count of the parameters used. This works in most cases, but when an external application executes a query using ADO.NET, or rows are inserted/deleted using Edit Top 200 Rows in SSMS, EventInfo doesn't have parameter values.

For example, if a string query is executed in a query window or as a non-parametrized string query in ADO.NET, EventInfo shows:

  INSERT INTO DTS_TABLE ([ID] ,[DTS_ID] ,[DTS] ,[TICS])  VALUES (10, 9, GETDATE(), 91234)  

When inserting the same data using Edit Top 200 Rows or a parametrized query in ADO.NET, EventInfo shows (newlines added for readability):

(@id int,@dtsid int,@dts datetime,@tics int)
INSERT INTO DTS_TABLE ([ID] ,[DTS_ID] ,[DTS] ,[TICS])  
VALUES (@id, @dtsid, @dts, @tics)

Is there anyway to access the parameter values within the context of my trigger?

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

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

发布评论

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

评论(1

古镇旧梦 2024-08-24 19:41:42

为什么要关心参数值?您应该关心的是表中的旧值和新值(在更新的情况下是旧值和新值 - 对于插入,仅是新值)。

由于您使用的是 SQL Server 2008,因此您应该查看新的审核功能。与使用触发器相比,有更多的细节可用,而且性能也大大提高。

此页面介绍了使用新功能时可用的信息。

以下是如何页面了解新功能。

Why would you care about the parameter values? All you should care about is the old and the new values in the table (old and new in the case of an update - for an insert, only new values).

Since you are using SQL Server 2008, you should look at the new Auditing feature. There is a lot more detail available, and the performance is much improved over using triggers.

Here is a page that describes the information available using the new feature.

Here is a how to page on the new feature.

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