Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL SELECT, store in a variable

For a stored procedure, I want to do a SELECT, and store a column's value into a variable.

How do I do this?

I want to do something like this:

    DECLARE countTemp INT;
    SET countTemp=(SELECT COUNT(Name) FROM mytable WHERE Name= var_name LIMIT 0,1);

OR, like this :

    DECLARE countTemp INT;
    SELECT countTemp=ColumnXYZ FROM mytable WHERE Name= var_name LIMIT 0,1;

But, I tried these and MySQL says my syntax is incorrect; how do I do something like this?

like image 443
rlb.usa Avatar asked Sep 15 '26 08:09

rlb.usa


1 Answers

Like this :

DECLARE myvar nvarchar(50);

SELECT ATextColumn INTO myvar FROM myTable LIMIT 1,1;

SELECT CONCAT('myvar is ',myvar ,' .');

http://www.java2s.com/Code/SQL/Procedure-Function/UseselectintotoassignvaluetoanIntegervariable.htm

like image 101
rlb.usa Avatar answered Sep 17 '26 02:09

rlb.usa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!