MYSQL, set two variables in a stored procedure using a single select statement

So, I would like it to be something like or have an effect:

declare vFN varchar(20); declare vLN varchar(20); set vFN, vLN = (select fname, lname from sometable where id = 1); 

Obviously, I could make 2 choices, but that seems very inefficient.

TIA

+8
variables mysql select procedure
source share
4 answers

try

 declare vFN varchar(20); declare vLN varchar(20); select fname, lname INTO vFN, vLN from sometable where id = 1; 

check http://dev.mysql.com/doc/refman/5.0/en/select-into.html for documentation.

+17
source share

They have two sets of statements. Set one using the select statement, and then copy the value in the first to the second.

 declare vFN varchar(20); declare vLN varchar(20); set vFN = (select fname, lname from sometable where id = 1); set vLN = vFN; 
+1
source share
 select vFN :=fname, vLN:=lname from sometable where id = 1 
+1
source share

Forgive me if this does not work in MySQL, because I use TSQL syntax. But you should be able to do something like:

 declare vFN varchar(20); declare vLN varchar(20); select vFN = fname, vLN = lname from sometable where id = 1; 

Or, if you need to make a choice in mysql:

 declare vFN varchar(20); declare vLN varchar(20); select fname into vFN, lname into vLN from sometable where id = 1; 
+1
source share

All Articles