5

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.

0

5 Answers 5

4

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;
Sign up to request clarification or add additional context in comments.

5 Comments

I tried using the NEW, but it says that it doesn't exist, the same goes for INSERTED.
INSERTED is for Sql Server. NEW should work for MySQL. Try to enclose your INSERT into a BEGIN / END block. I have updated my answer. See the MySql reference manual for the description and examples: dev.mysql.com/doc/refman/5.0/en/create-trigger.html
I tried the updated code, and it gives me the error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5.
Maybe it was the missing semicolons? I'm not an MySQL expert :)
Hi Jan, I actually tried adding the semicolons myself, but it's still giving me the same error, it just moves the line number up. I appreciate the help though.
3

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 ;

Comments

1

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.

2 Comments

Hi Will, will this work for MySQL? I don't have a ton of experience with it, when I tried using this it told me the syntax wasn't correct on the instead of line.
I've tried that, it doesn't resolve any of the issues. I've played around with the syntax a bit.
1

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.

2 Comments

Besides, will this guarantee data integrity, that is, if one of the inserts fail, what happens? Is it possible that one insert will be performed without the other one being successful? That is the concern of the transaction in my code sample. And sorry, I didn't see you were using MySQL.
I do not want it to insert into the second table if the first fails, the mbb_messages table is to keep a log of messages that have been sent out, as the mbb_pushNotificationQueue will be cleared every so often.
0

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.

1 Comment

No, thats very dangerous. Every rdbms gives you the inserted records via a special table or a REFERENCES clause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.