¿Cómo seleccionar el valor de retorno de la declaración preparada de mysql?

I am writing a stored procedure in MySQL. The following is the code I wrote:

SET @qry = 'SELECT id into @wid FROM work_profile where candidate_id = 1223 limit 1';
PREPARE statement FROM @qry;
EXECUTE statement;

Sin embargo, @wid is null after execution.

SELECT id FROM work_profile where candidate_id = 1223 limit 1 

This returns a value of 1443.

preguntado el 08 de noviembre de 11 a las 13:11

Does table work_profile contain any data? Try to execute 'SELECT id FROM work_profile'. Please show full code. How do you use @wid variable? -

please see the edited question -

...and, how do you use @wid variable? -

4 Respuestas

Lo siguiente funciona para mí:

USE test;
DELIMITER $$
CREATE PROCEDURE GETNID()
BEGIN
  SET @query = 'SELECT 100 INTO @nid';
  PREPARE statement1 FROM @query;
  EXECUTE statement1;
END$$
DELIMITER ; 

And to call the procedure:

CALL GETNUMBER();
SELECT @nid;

However, this does not seem to work in the MySQL Query Browser. I had to resort to using the MySQL command line client. I suspect the Query Browser clears the session after each statement.

For more EXECUTE examples, see the MySQL manual:

respondido 08 nov., 11:18

The query may only return one row for use in this syntax. Either specify a WHERE clause which returns one row, or add LIMIT 1

respondido 08 nov., 11:17

But I added where and limit even though its returning null - Anand

Prueba esta

set @qry := 'test';
execute statement using @qry

respondido 08 nov., 11:17

Remove the INTO @wid from your query and add LIMIT 1. The query will return a single row with the value you want.

respondido 08 nov., 11:18

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.