sql触发器将行插入两个表

发布于 2024-10-08 20:31:49 字数 330 浏览 0 评论 0原文

我正在寻找一种创建触发器的方法,该触发器会将同一行插入具有相同值的两个表中。

例如,插入新行后立即将其插入到pushNotificationQueue中,我希望将同一行插入到消息中。

我尝试了这个

CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW
insert into mbb_messages select * from mbb_pushNotificationQueue

,唯一的问题是它会遍历并添加之前已添加的条目。

I was looking for a way to create a trigger that would insert the same row into two tables with the same values.

For example, a new row is inserted into pushNotificationQueue as soon as that is inserted, I would like that same exact row to be inserted into messages.

I tried this

CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW
insert into mbb_messages select * from mbb_pushNotificationQueue

the only problem with that is that it goes through and adds entries that have already been previously added.

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

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

发布评论

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

评论(5

在你怀里撒娇 2024-10-15 20:31:50

所以只添加最后添加的记录。

insert into mbb_messages select blah from mbb_pushNotificationQueue where blah meets some criteria....

有 max(id) 之类的。

So only add the last record added.

insert into mbb_messages select blah from mbb_pushNotificationQueue where blah meets some criteria....

Having max(id) or something.

我家小可爱 2024-10-15 20:31:49

您必须说明您正在使用哪个 RDBMS。
无论如何,您必须使用一个通常命名为 insert 或类似名称的特殊表。

这是针对 Sql Server 的:

INSERT INTO mbb_messages SELECT * FROM INSERTED

Sybase 等其他服务器使用 REFERENCES 子句来获取新插入的记录:

create trigger TriggerName after insert on
TableName
referencing new as new_name

对于 MySQL(您似乎正在使用),您可以使用 NEW 表来引用新插入的记录:

CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW BEGIN
    insert into mbb_messages select * from NEW;
END;

You have to say with which rdbms you are working.
Anyway, you have to use a special table normally named inserted or similar.

This is for Sql Server:

INSERT INTO mbb_messages SELECT * FROM INSERTED

Others like Sybase use a REFERENCES clause to get to the newly inserted record:

create trigger TriggerName after insert on
TableName
referencing new as new_name

And for MySQL (which you are seem to use) you can refer to the newly inserted records by using the NEW table:

CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW BEGIN
    insert into mbb_messages select * from NEW;
END;
書生途 2024-10-15 20:31:49

您需要将列名与 new 关键字一起使用。请在下面找到触发器:

DELIMITER $
CREATE TRIGGER add_to_message
after insert on mbb_pushNotificationQueue
FOR EACH ROW BEGIN
    insert into mbb_oushNotificationQueue(`col1`, `col2`) values(new.col1, new.col2);
END$
DELIMITER ;

You need to use the column name with new keyword. Please find the trigger below:

DELIMITER $
CREATE TRIGGER add_to_message
after insert on mbb_pushNotificationQueue
FOR EACH ROW BEGIN
    insert into mbb_oushNotificationQueue(`col1`, `col2`) values(new.col1, new.col2);
END$
DELIMITER ;
挽心 2024-10-15 20:31:49

首先,我要说的是,将 select *insert-select 语句一起使用确实是一个坏主意。原因是您永远无法预测从选择中返回的列的顺序。

其次,假设使用 SQL Server,我建议使用以下内容:

create trigger add_to_message
    instead of insert on mbb_pushNotificationQueue
    for each row 
as
    begin transaction
        insert into mbb_oushNotificationQueue (col1, col2, col3)
            select col1, col2, col3
                from inserted

        insert into mbb_messages (col1, col2, col3)
            select col1, col2, col3
                from inserted

       if @@ERROR_LEVEL = 0
           commit
      else
           rollback

免责声明:

此代码尚未经过测试,可能需要一些小的修复,但很好地说明了这个想法。

First of all, I say that using select * with an insert-select statement is really, really a bad idea. The reason is that you can never predict the order of the columns that are returned from a selection.

Secondly, assuming SQL Server, I would suggest using the following:

create trigger add_to_message
    instead of insert on mbb_pushNotificationQueue
    for each row 
as
    begin transaction
        insert into mbb_oushNotificationQueue (col1, col2, col3)
            select col1, col2, col3
                from inserted

        insert into mbb_messages (col1, col2, col3)
            select col1, col2, col3
                from inserted

       if @@ERROR_LEVEL = 0
           commit
      else
           rollback

Disclaimer:

This code has not been tested and may require some minor fixes, but is illustrating the idea very well.

抠脚大汉 2024-10-15 20:31:49

我最终使用了

CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW
INSERT INTO mbb_messages SET messageID = NEW.messageID, 
toUserID = NEW.toUserID, 
fromUserID =  NEW.fromUserID, message = NEW.message, dateReceived = NEW.dateReceived

感谢所有发帖的人。

I ended up using

CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW
INSERT INTO mbb_messages SET messageID = NEW.messageID, 
toUserID = NEW.toUserID, 
fromUserID =  NEW.fromUserID, message = NEW.message, dateReceived = NEW.dateReceived

Thanks to everyone who posted.

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