调用触发器时,将行从一个表复制到另一个表 [英] Copying row from one table to another when trigger is called
本文介绍了调用触发器时,将行从一个表复制到另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2张桌子
A)
Table A
ID | val |
1. 1 m
2. 2 n
3. 1 o
4 1 p
5. 2 q
Table B
ID | val
所以我有一个要求,当任何行被插入表A中时,相同的数据将被更新表B.我正在尝试使用触发器。我的触发器看起来像
So I have a requirement where when any row get's inserted into table A the same data get's updated in table B. This I am trying to do using a trigger.My trigger looks like
CREATE Trigger tr_ForInserts
ON A
FOR INSERT
AS
BEGIN
SET NOCOUNT ON;
insert into B values(NEW.ID,NEW.Val);
END
我面临的问题是如何识别表格中的哪一行插入了A,因为我对单个ID有重复的条目。在触发触发器之后,我将如何检测插入的确切行。查询插入B值(NEW.ID,New.Val)中的参数应该是什么.....这些新值应该是什么?
The problem which I am facing is How I"ll identify which row in table A is inserted as I have duplicate entry for single ID.How I"ll detect the exact row which got inserted after trigger has been called. what should be the parameter in query "insert into B values(NEW.ID,New.Val)".....what should be these New value?
推荐答案
CREATE TRIGGER tg_insert ON A
FOR INSERT
AS
BEGIN
INSERT B
SELECT id, val
FROM inserted
END
这篇关于调用触发器时,将行从一个表复制到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文