0

I have create a trigger which is create a dynamic query.and execute it i had tried 'EXECU q' but it does not work. how can i run/execute that dynamic query.

BEGIN
    DECLARE a INT Default 0 ;
    DECLARE str  VARCHAR(255);
    DECLARE q VARCHAR(500);

    SET q = 'insert into '+new.master_name+' values(';

    simple_loop: LOOP
        SET a=a+1; 
        SET str = SPLIT_STRING(new.remarks,"|",a); 
        SET q = CONCAT(q,str+',');
        SET q = LEFT(q, LENGTH(q) - 1);
        IF str='' THEN
                LEAVE simple_loop; 
            END IF;


    END LOOP simple_loop; 
    SET q = CONCATE(q,');');

    EXEC q
END

This is Trigerr this is Function which i made RETURN REPLACE( SUBSTRING( SUBSTRING_INDEX(str , delim , pos) , CHAR_LENGTH( SUBSTRING_INDEX(str , delim , pos - 1) ) + 1 ) , delim , '' )

5
  • use prepare statement Commented May 29, 2018 at 9:26
  • You should add the split_string function (which I don't think works) Commented May 29, 2018 at 10:04
  • i have add function which i made Commented May 29, 2018 at 10:11
  • That's not going to work dynamic sql is not allowed in a trigger or stored function. Commented May 29, 2018 at 11:36
  • what the option for if Commented May 29, 2018 at 12:29

3 Answers 3

5

I've written a stored procedure to execute dynamically constructed sql statements.

Usage

SET @index := 7;
CALL eval(CONCAT('SELECT ', @index));

Implementation

DELIMITER $$

CREATE PROCEDURE eval(IN dynamic_statement TEXT)
  BEGIN
      SET @dynamic_statement := dynamic_statement;
      PREPARE prepared_statement FROM @dynamic_statement;
      EXECUTE prepared_statement;
      DEALLOCATE PREPARE prepared_statement;
  END$$

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

Comments

0

From my understanding you must make a prepared statement from your string first in order to execute it. So the following partial code should work in replacement for just EXEC q:

PREPARE thequery FROM q;
EXECUTE thequery;

1 Comment

i have tried this also but it does not work. give error at PREPARE thequery FROM q;
0

use prepare statement to execute your dynamic query

BEGIN
    DECLARE a INT Default 0 ;
    DECLARE str  VARCHAR(255);
    DECLARE q VARCHAR(500);
    DECLARE q1 VARCHAR(500);
    DECLARE q2 VARCHAR(500);

    SET @q = 'insert into '+new.master_name+' values(';

    simple_loop: LOOP
        SET a=a+1; 
        SET str = SPLIT_STRING(new.remarks,"|",a); 
        SET q = CONCAT(@q,str+',');
        SET q1 = LEFT(q, LENGTH(q) - 1);
        IF str='' THEN
                LEAVE simple_loop; 
            END IF;


    END LOOP simple_loop; 

    SET q2 = CONCATE(q1,');');
    PREPARE stmt FROM q2;
    EXECecute stmt;
    deallocate PREPARE stmt;
END

4 Comments

is dynamic sql is work in trigger or store producer ?
i have made changes ...try them
Still its will not allow at line PREPARE stmt FRom q2
What the meaning of @ notation? when we use it

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.