2

i am getting exception while create a trigger on mysql, Pasting piece of my trigger below. I hope the prepare statement in this trigger throws this exception, Please correct me anything error in syntax.

 DELIMITER $$

 CREATE DEFINER=`root`@`localhost` TRIGGER mysql_common.mysql_alert_trigger after insert on mysql_common.alert FOR EACH ROW

 BEGIN

 set @col_string = "col1,col2,col3";
 set @val_string = "('val1','val2','val3')";

 SET @s := CONCAT('INSERT into   msql_common.seperated_processlist(',@col_string,') values ', @val_string);
 PREPARE dynamic_statement FROM @s;
 EXECUTE dynamic_statement;
 DEALLOCATE PREPARE dynamic_statement;

 END $$
 DELIMITER ;
8
  • What are you trying to accomplish in this trigger? Why are you constructing a dynamic SQL statement? (This is rather unusual and risky!) Commented Apr 27, 2017 at 7:21
  • I need this trigger for a application data. Commented Apr 27, 2017 at 7:26
  • That really doesn't explain what you're trying to do. Can you give a more representative example? (Surely your trigger isn't inserting the same data every time it's activated…) Commented Apr 27, 2017 at 7:27
  • that is sample trigger, there is no original data Commented Apr 27, 2017 at 9:09
  • SQL prepared statements (PREPARE, EXECUTE, DEALLOCATE PREPARE) can be used in stored procedures, but not stored functions or triggers.. See C.1 Restrictions on Stored Programs. Commented Apr 27, 2017 at 9:12

1 Answer 1

3
DELIMITER $$

 CREATE DEFINER=`root`@`localhost` TRIGGER mysql_common.mysql_alert_trigger after insert on mysql_common.alert FOR EACH ROWEACH ROW

 BEGIN

 call SOME_PROC();
 END $$
 DELIMITER ;

You can wrote one procedure with your business logic and call that procedure from trigger as shown in above code.with your specific input and output parameter.

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `SOME_PROC`()
BEGIN
    set @col_string = "col1,col2,col3";
 set @val_string = "('val1','val2','val3')";

 SET @s := CONCAT('INSERT into   msql_common.seperated_processlist(',@col_string,') values ', @val_string);
 PREPARE dynamic_statement FROM @s;
 EXECUTE dynamic_statement;
 DEALLOCATE PREPARE dynamic_statement;

END$$
DELIMITER ;

Hope this will helps.

Sign up to request clarification or add additional context in comments.

8 Comments

While you can create a prepared statement in a stored procedure, in the context of a trigger, you can't call a stored procedure that executes a prepared statement, the error indicated in the question will be thrown.
@wchiquito i had tried same.It's working for my end.
Does your stored procedure (with the prepared statement) run correctly from the trigger?.
@wchiquito Yes it it.
What version of MySQL are you using?.
|

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.