Friday, April 27, 2012

How to assign select query result to a variable?



CREATE PROCEDURE getName (x VARCHAR(255))
BEGIN
DECLARE var1 VARCHAR(255);
SELECT name INTO var1 FROM table1 WHERE name = x ; /* >>> INTO var1 <<< */
SELECT var1;
END;

or

CREATE PROCEDURE `p`(x VARCHAR(255))
BEGIN
DECLARE var1 VARCHAR(255);
SET var1 = (SELECT name FROM table1 WHERE name = x);
SELECT var1;
END$$

http://forums.mysql.com/read.php?98,132871,132883#msg-132883

No comments: