SQL Server 2005+
我有一个带有 INSTEAD OF INSERT
触发器的 View 。在触发器体内,我想使用带有 OUTPUT
子句的语句,该子句引用两个 INSERTED
表:
INSTEAD OF INSERT
触发器的外部INSERTED
表OUTPUT
子句的内部INSERTED
表
MSDN says this :
If a statement that includes an OUTPUT clause is used inside the body of a trigger, table aliases must be used to reference the trigger inserted and deleted tables to avoid duplicating column references with the INSERTED and DELETED tables associated with OUTPUT.
但别名似乎不起作用:
CREATE TRIGGER v_insert ON v
INSTEAD OF INSERT
AS BEGIN
INSERT INTO t (a, b, c)
OUTPUT inserted.a, inserted.b, outer_inserted.d INTO t_prime (a, b, d)
SELECT a, b, c
FROM inserted as outer_inserted
END
它会产生错误“无法绑定(bind)多部分标识符“outer_inserted.d”。这是否意味着我尝试做的事情是不可能的?
最佳答案
我读它是因为在访问触发器 INSERTED 的 FROM 中需要 INSERTED 别名。
OUTPUT 子句中的 INSERTED 只能引用插入到 t 中的数据。
所以你的 OUTPUT 子句中不能有 outer_inserted.d
你也不能这样做,我就是这么读的
INSERT INTO t (a, b, c)
OUTPUT inserted.a, inserted.b INTO t_prime (a, b)
SELECT a, b, c
FROM inserted --no alias = **FAIL**
关于sql-server - 在 INSTEAD OF INSERT 触发器的 OUTPUT 子句中,是否可以引用两个 INSERTED 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3991183/