如果数据尚不存在,则使用触发器插入表中
我有两个具有相同结构的表。表 1 有多行可以具有相同的值。现在我想将相同的行插入到表 2 中,排除重复的行。我通常可以使用“减号”来执行此操作,但我想编写一个触发器,以便如果将新行插入到表 1 中并且不存在于表 2 中,则插入到表 2 中,否则不插入。我是触发器新手。当我插入表 1 时,我编写的触发器给出了“触发器正在变异”错误。
INSERT INTO t3(name1,name2,num1,num2) select name1,name2,num1,num2 from t1 group by name1,name2,num1,num2 minus select * from t3
当我编写上面的代码时,它工作正常,但是当我包含此代码时到我的触发器中它给出了错误。如何在触发器的帮助下执行上述操作?
请帮忙,
谢谢
普拉奈
I have two tables with the same structure. Table 1 has multiple rows which can have same values. Now i want to insert the same rows into table 2 excluding duplicate rows. I am able to do this normally using 'minus', but i want to write a trigger such that if a new row is inserted into table 1 and is not present in table 2 then insert in table 2 otherwise not. I am new to triggers. The trigger i have written gives me "trigger is mutating" error when i insert in table 1.
INSERT INTO t3(name1,name2,num1,num2) select name1,name2,num1,num2 from t1 group by name1,name2,num1,num2 minus select * from t3
when i write the above code it works fine but when i include this into my trigger it gives error. How do i perform the above with the help of a trigger?
Please help,
Thanks
Pranay
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要从行级触发器重新查询表。这就是
:NEW.
语法的用途,例如:虽然我认为上面的代码看起来有点傻。我更愿意对
t3
施加唯一约束,然后在触发器中添加处理程序来处理任何DUP_VAL_ON_INDEX
异常。You don't need to requery the table from a row-level trigger. That's what the
:NEW.
syntax is for, e.g.:Although I think the above code looks a bit silly. I'd prefer to put a unique constraint on
t3
then add a handler in the trigger to take care of anyDUP_VAL_ON_INDEX
exceptions.