I need to do a multiple insert in a stored procedure with MySQL. I've been looking about this but I didn't find almost any information or examples at all.
The number of messages (inserts) varies. Sometimes could be one, sometimes could be twenty.
In short, I need to do the following: let's say the users could belong to a group. If one of them leave the group, I need to advice about this action to the other users with a message.
The inserts would be something like this:
INSERT INTO messages (author, dest, title, msg)
VALUES (ldr,id1,'Same title for all messages','Same body message for all'),
(ldr,id2,'Same title for all messages','Same body message for all'),
...
I've tried to do it with single inserts into a loop but the time it lasts is, as you can guess, terribly high.
The possible ways I've thought or seen in other pages are:
- Take the ID's of the users into a text variable, prepare the insert statement with PREPARE and execute it.
- Take the ID's of the users, create a temporary table and introduce the ID's, and finally insert all the messages from there.
- Any other way to do it...¿?
I don't know which one of them is the best way to do it, in the sense of good practice and performance/efficiency.
Do you have any ideas?
In case you know how to do it, please, could you write a simple example?
Thank you so much.