SQL Command in String MySQL

3

I'm doing a procedure that holds a select in a varchar variable. Now I need to run this sql command, how do I do this? I'm using MySql. Procedure:

delimiter $$
create procedure eixos_caminhao (in numeroEixos int)
declare comando varchar(500);
    set comando = 'select classe, velocidade, date(data) as data, pesoEixo1, pesoEixo2';
    if (numeroEixos > 2) set comando = concat(comando, ', pesoEixo3');
    if (numeroEixos > 3) set comando = concat(comando, ', pesoEixo4');
    if (numeroEixos > 4) set comando = concat(comando, ', pesoEixo5');
    if (numeroEixos > 5) set comando = concat(comando, ', pesoEixo6');
    if (numeroEixos > 6) set comando = concat(comando, ', pesoEixo7');
    if (numeroEixos > 7) set comando = concat(comando, ', pesoEixo8');
    if (numeroEixos > 8) set comando = concat(comando, ', pesoEixo9');
    set comando = concat(comando. ' from tb_vbv');
end$$
delimiter;
    
asked by anonymous 09.10.2018 / 17:42

2 answers

3

Use the commands PREPARE and EXECUTE :

PREPARE myquery FROM comando;
EXECUTE myquery;

You can search more links like this .

delimiter $$
create procedure eixos_caminhao (in numeroEixos int)
begin
    set @comando := 'select classe, velocidade, date(data) as data, pesoEixo1, pesoEixo2';
    if (numeroEixos > 2) set @comando:= concat(@comando, ', pesoEixo3');
    if (numeroEixos > 3) set @comando:= concat(@comando, ', pesoEixo4');
    if (numeroEixos > 4) set @comando:= concat(@comando, ', pesoEixo5');
    if (numeroEixos > 5) set @comando:= concat(@comando, ', pesoEixo6');
    if (numeroEixos > 6) set @comando:= concat(@comando, ', pesoEixo7');
    if (numeroEixos > 7) set @comando:= concat(@comando, ', pesoEixo8');
    if (numeroEixos > 8) set @comando:= concat(@comando, ', pesoEixo9');
    set @comando:= concat(@comando, ' from tb_vbv');

    PREPARE myquery FROM @comando;
    EXECUTE myquery;
end;
delimiter;
    
09.10.2018 / 17:52
2

To execute a dynamic command within a function , procedure or trigger it is necessary to prepare the string and then run it.

For example, to create a procedure that sums two numbers and returns the result would look something like this:

DELIMITER $$

DROP PROCEDURE IF EXISTS soma $$ 

CREATE PROCEDURE soma(in valor1 INT(11), in valor2 INT(11))
BEGIN
    PREPARE stmt FROM CONCAT('SELECT ', valor1,' + ', valor2, ' AS resultado');
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END $$

DELIMITER;

To return the result just execute something like:

CALL soma(1, 2);

In this case the return will be:

+---------+
|resultado|
|---------|
|        3|
+---------+

This same function can also be changed so that parameters are passed. The advantage of passing parameters is that you can reuse the same query at some point within procedure .

DELIMITER $$

DROP PROCEDURE IF EXISTS soma $$ 

CREATE PROCEDURE soma(in valor1 INT(11), in valor2 INT(11))
BEGIN
    SET @valor1 = valor1, @valor2 = valor2; # É necessário criar essas variáveis antes de utiliza-las no USING
    PREPARE stmt FROM 'SELECT ? + ? AS resultado';
    EXECUTE stmt USING @valor1, @valor2;
    DEALLOCATE PREPARE stmt;
END $$

DELIMITER;

If you pass the same parameters the return will be exactly the same.

You can see more about prepared statments here .

Explanation of Commands

  • PREPARE nome_da_variavel FROM sql : At this point query is being prepared, that is, the variable nome_da_variavel will receive a statment with the query that was passed after FROM ;
  • EXECUTE nome_da_variavel : EXECUTE will execute statment . If you have parameters, after statment you must use the reserved word USING and after that, all the parameters separated by commas.
  • DEALLOCATE PREPARE nome_da_variavel : DEALLOCATE is used to release the variable, that is, after running it, the statment will no longer exist.
09.10.2018 / 18:51