Reputation:
Can a stored procedure plus a sql query be executed from within a trigger?
procedure plus query
SET @tmp = (SELECT fvalue FROM ftable WHERE id=2);
SET @SQL = CONCAT('INSERT INTO ',@tmp,' VALUES (''buhambug'')');
PREPARE stmt FROM @SQL;
EXECUTE stmt;
If so what are the rules and links to examples? I have't been able to find any.
Upvotes: 2
Views: 87
Reputation: 5173
Yeah, you can call a stored procedure from a trigger like this:
Create trigger foo after insert on table for each row
BEGIN
call your_procedure(params);
END$$
Note the ending delimiter. If ;
is the default delimiter inside the trigger as well, then it won't work, for MySQL will treat each command separately and throw error. You want the entire code inside the trigger to be executed together. hence declare a different delimiter, like $$ prior to defining the trigger, through Delimiter $$
. Then, ;
will work correctly inside the the trigger. After you terminate the trigger through $$, don't forget to restore the default delimiter.
Upvotes: 2