Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8574953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:35:24+00:00 2026-06-11T19:35:24+00:00

Mysql PREPARE and EXECUTE statements can not be used in a stored procedure that

  • 0

Mysql PREPARE and EXECUTE statements can not be used in a stored procedure that is being called by a trigger. The result would be Error Code: 1336. Dynamic SQL is not allowed in stored function or trigger.

Does anyone know a plausible work around to this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:35:25+00:00Added an answer on June 11, 2026 at 7:35 pm

    You can’t run PREPARE/EXECUTE from inside a TRIGGER, but you can from an EVENT (if you are running MySQL 5.5 or greater).

    Here’s an example of running PREPARE/EXECUTE from an EVENT:

    DROP TABLE IF EXISTS tbl1;
    DROP TABLE IF EXISTS tbl2;
    DROP TABLE IF EXISTS cmds;
    DROP PROCEDURE IF EXISTS proc;
    DROP TRIGGER IF EXISTS trig;
    
    CREATE TABLE tbl1 (i INT, v VARCHAR(255));
    CREATE TABLE tbl2 (i INT, v VARCHAR(255));
    
    CREATE TABLE cmds (
        id INT UNSIGNED NOT NULL AUTO_INCREMENT,
        done BOOL NOT NULL DEFAULT FALSE,
        cmd TEXT,
        PRIMARY KEY (id),
        INDEX (done, id)
    );
    
    DELIMITER //
    
    CREATE PROCEDURE proc()
    NOT DETERMINISTIC
    MODIFIES SQL DATA
    proc: BEGIN
        DECLARE b_not_found     BOOL DEFAULT FALSE;
        DECLARE i_id            INT UNSIGNED;
        DECLARE t_cmd           TEXT;
        DECLARE v_lock_name     VARCHAR(255) DEFAULT 'proc_lock';
    
        DECLARE cur CURSOR FOR
            SELECT id, cmd FROM cmds WHERE NOT done ORDER BY id;
    
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET b_not_found = TRUE;
    
        IF (NOT GET_LOCK(v_lock_name, 0)) THEN
            LEAVE proc;
        END IF;
    
        OPEN cur;
    
        loop1: LOOP
            FETCH cur INTO i_id, t_cmd;
            IF b_not_found THEN
                LEAVE loop1;
            END IF;
    
            SET @cmd = t_cmd;
    
            PREPARE stmt FROM @cmd;
            EXECUTE stmt;
            DROP PREPARE stmt;
    
            UPDATE cmds SET done = TRUE WHERE id = i_id;
        END LOOP;
    
        CLOSE cur;
    
        DO RELEASE_LOCK(v_lock_name);
    END;
    //
    
    CREATE TRIGGER trig
        BEFORE INSERT ON tbl1
        FOR EACH ROW
    BEGIN
        INSERT INTO cmds SET cmd = 
            CONCAT("INSERT INTO tbl2 SET i = ", -NEW.i, ", v = ", QUOTE(NEW.v));
    END;
    //
    
    DROP EVENT IF EXISTS evnt //
    
    CREATE EVENT evnt
    ON SCHEDULE 
    EVERY 1 SECOND
    DO
    BEGIN
        CALL proc();
    END;
    //
    
    DELIMITER ;
    
    SET GLOBAL event_scheduler = 1;
    

    Then running this:

    INSERT INTO tbl1 VALUES (UNIX_TIMESTAMP(), 'ex 1');
    DO SLEEP(2);
    INSERT INTO tbl1 VALUES (UNIX_TIMESTAMP(), 'ex 2');
    DO SLEEP(1);
    SELECT * FROM tbl2;
    

    will produce this output:

    +-------------+------+
    | i           | v    |
    +-------------+------+
    | -1348550619 | ex 1 |
    | -1348550621 | ex 2 |
    +-------------+------+
    2 rows in set (0.00 sec)
    

    If you don’t want to use an EVENT, or wait the second or so for it to fire, you could add a CALL proc() after every command that would cause a TRIGGER to fire.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Mysql Version 14.14 Distrib 5.1.41 I have a stored procedure that I am trying
I was wondering if there was any difference between the MySQL PREPARE/EXECUTE clauses and
Are PHP/mysql prepared statements possible when mysqli and PDO are not available? Are there
I learned today through this section of the MySQL documentation that prepared statements cannot
I am using prepared statements to execute mysql database queries. And I want to
How can I use dynamic SQL statements in MySQL database and without using session
I am converting some of my code that used ext/mysql ( mysql_*() functions) to
Here is my problem, Not using prepared statements I can do it just fine,
In MySQL is a prepared statement inside a stored procedure safe from SQL injection?
So i am trying to build and prepare using PDO, a mysql query dynamically.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.