sql-server - 在 INSTEAD OF INSERT 触发器的 OUTPUT 子句中,是否可以引用两个 INSERTED 表?

标签 sql-server triggers

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/

相关文章:

上一行的 MySQL 触发器

sql-server - 数据流问题

triggers - 如何为 redis 数据存储实现 "trigger"?

mysql - 插入后触发将所有行的副本写入审核文件

sql-server - SQL Server 中用于别名的方括号和单引号有什么区别?

mysql - 删除触发器后出错

javascript - 使用 JavaScript 事件再次触发 CSS 动画

sql - 使用内连接和字符串插值更新列

sql - ms sql 上 SP 性能问题中的 UDF

c# - 如何从 .NET 以编程方式调用 SSIS?