触发器:插入的表格如何工作?如何访问其行? [英] Trigger: How does the inserted table work? How to access its rows?

查看:60
本文介绍了触发器:插入的表格如何工作?如何访问其行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表

Data --Table name
ID -- Identity column
PCode -- Postal Code

我创建了以下触发器:

CREATE TRIGGER Trig
ON Data
FOR INSERT
AS
BEGIN 

    Select * from inserted

END

并插入以下值

INSERT INTO Data VALUES (125)
INSERT INTO Data VALUES (126)
INSERT INTO Data VALUES (127)

它显示以下内容:

但是我期待这样的事情:

But I was expecting something like this:


  • 第一次插入后,执行触发器->在插入的表中显示一行。

  • 第二次插入后,触发 执行->在插入的表中显示两行。

  • 在第3次插入后,执行触发器->三行在插入的表中显示。

  • After the 1st insertion, the trigger is executed -> one row is shown in the inserted table.
  • After the 2nd insertion, the trigger is executed -> two rows are shown in the inserted table.
  • After the 3rd insertion, the trigger is executed -> three rows are shown in the inserted table.

根据 msdn.microsoft 所有插入的行都在此表中。

According to msdn.microsoft all the rows inserted are in this table.

如何访问插入的表,以便

推荐答案

您不能看到。从使用microsoft.com上插入和删除的表文章,您可以阅读:

You can not. From the Use the inserted and deleted Tables article on microsoft.com, you can read:


插入的表在存储期间存储了受影响的行的副本INSERT和UPDATE语句。

The inserted table stores copies of the affected rows during INSERT and UPDATE statements.

这意味着插入的表将仅包含当前 INSERT UPDATE 语句的行。

That means that the inserted table will only contain rows for the current INSERT or UPDATE statement.

如果确实要查看几个这样的 INSERT UPDATE 语句的所有行,则必须将这些行存储在您自己创建的表格。

If you do want to see all rows for several such INSERT or UPDATE statements, you will have to store these rows in a table you created yourself.

这篇关于触发器:插入的表格如何工作?如何访问其行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆