从插入触发器后调用存储过程
也许是一个愚蠢的问题!
如果我从插入后触发器 (T-SQL) 调用存储过程 - 那么如何获取“刚刚插入”数据的值? 例如,
CREATE TRIGGER dbo.MyTrigger
ON dbo.MyTable
AFTER INSERT
AS
BEGIN
EXEC createAuditSproc 'I NEED VALUES HERE!'
我不需要担心任何标识列 - 我只想使用一些“刚刚插入”的值传递到我的存储过程中。
编辑:为了澄清 - 我需要它来调用存储过程而不是直接插入表,因为存储过程执行不止一件事。我正在使用一些遗留表,目前无法修改以“正确”执行操作(时间/资源/遗留代码),所以我必须使用我所拥有的:(
Perhaps a stupid question!
If I call a stored proc from an After Insert trigger (T-SQL) - then how do I get the values of the "just inserted" data?
e.g.
CREATE TRIGGER dbo.MyTrigger
ON dbo.MyTable
AFTER INSERT
AS
BEGIN
EXEC createAuditSproc 'I NEED VALUES HERE!'
I don't have any identity columns to worry about - I just want to use some of the "just inserted" values to pass into my sproc.
Edit: For clarification - I need this to call a sproc and not do a direct insert to the table, since the sproc does more than one thing. I'm working with some legacy tables I can't currently amend to do things 'properly' (time/resource/legacy code), so I have to work with what I have :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 INSERTED 和 DELETED 获取新“更改”的数据伪表:
给出示例表
编辑:抱歉,我没有解决有关调用存储过程的问题。根据 marc_s 的评论,请注意插入/删除可以包含多行,这使存储过程变得复杂。就我个人而言,我会让触发器直接插入审计表,而不封装 SPROC。但是,如果您有 SQL 2008,则可以使用表值参数,如下所示:
然后您的触发器将更改为如下所示:
然后您可以测试这是否适用于单个和多个插入
但是,如果您使用 SQL 2005 或更低版本,您可能需要使用游标来循环插入传递行到您的 SPROC,这太可怕了,无法想象。
顺便说一句,如果您有 SQL 2008,您可以查看更改数据捕获
编辑#2:由于您需要调用过程,并且如果您确定只插入一行...
You get to the newly 'changed' data by using the INSERTED and DELETED pseudo-tables:
Given the example tables
Edit : Apologies, I didn't address the bit about calling a Stored Proc. As per marc_s's comment, note that inserted / deleted can contain multiple rows, which complicates matters with a SPROC. Personally, I would leave the trigger inserting directly into the audit table without the encapsulation of a SPROC. However, if you have SQL 2008, you can use table valued parameters, like so:
And then your trigger would be altered as like so:
you can then test that this works for both a single and multiple inserts
However, if you are using SQL 2005 or lower, you would probably need to use a cursor to loop through inserted passing rows to your SPROC, something too horrible to contemplate.
As a side note, if you have SQL 2008, you might look at Change Data Capture
Edit #2 : Since you need to call the proc, and if you are certain that you only insert one row ...