sql server触发器引用最后一行

发布于 2024-10-05 05:03:58 字数 338 浏览 0 评论 0原文

我想在 SQL Server 中编写一个触发器,以便在向 table1 中插入一行时,向 table2 中插入 2 行。我想使用 table1 中的列值。

所以我的触发器看起来像这样

create trigger triggername on table1
as
begin
insert into
insert into
end

How do I get the value of any columns from the Last插入的行(触发触发器的行插入)。即相当于oracle中的“引用行”

I want to write a trigger in SQL Server to insert 2 rows into table2 when a row is inserted into table1. I want to use a column value from table1.

So my trigger looks like this

create trigger triggername on table1
as
begin
insert into
insert into
end

How do I get the value of any column from the last inserted row (the row insertion which fires the trigger). I.e. the equivalent of 'referencing row' in oracle

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

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

发布评论

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

评论(2

锦爱 2024-10-12 05:03:59

SQL Server 中的触发器按语句而不是按行触发。您可以使用两个伪表 inserteddeleted(对于 insert 触发器,唯一感兴趣的是 inserted代码>)

CREATE TRIGGER YourTrigger ON Table1
FOR INSERT
AS
INSERT INTO Table2 
SELECT * from inserted /*This will contain multiple rows if it is a multi-row insert*/

Triggers in SQL Server fire per statement not per row. There are two pseudo tables inserted and deleted that you can use (for an insert trigger the only one of interest is inserted)

CREATE TRIGGER YourTrigger ON Table1
FOR INSERT
AS
INSERT INTO Table2 
SELECT * from inserted /*This will contain multiple rows if it is a multi-row insert*/
辞旧 2024-10-12 05:03:59

你好
我自己有解决方案。我错过了别名

select @patent_no=fldL1BasCode from insert

应该是

select @patent_no=i.fldL1BasCode from insert i

hi
i have the solution myself. i missed the alias

select @patient_no=fldL1BasCode from inserted

should be

select @patient_no=i.fldL1BasCode from inserted i

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