MySQL SELECT, store in variable
For a stored procedure, I want to do a SELECT and store the column value in a variable.
How to do it?
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, for example:
DECLARE countTemp INT;
SELECT countTemp=ColumnXYZ FROM mytable WHERE Name= var_name LIMIT 0,1;
But, I tried this, and MySQL says my syntax is wrong; how can I do something like this?
+2
a source to share
1 answer
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
+2
a source to share