how to declare local variable in mysql

时间:2015-06-26 10:17:39

标签: java mysql database

declare my_variable varchar(250)
 set    my_variable= SELECT  Quote( lower(substring_index (doctype, ' ', 1))) from dms_report  LIMIT 1 ;
select my_variable;

I am trying run this Query but I am getting an Error on declare and when execute the query I am not getting the results while SELECT Quote( lower(substring_index (doctype, ' ', 1))) from this Query returning some value but I am unable to store it in my_variable please help me where am doing wrong how to declare variable

please suggest me

1 个答案:

答案 0 :(得分:0)

You need parentheses around the subquery:

declare my_variable varchar(250);
set my_variable = (SELECT Quote(lower(substring_index(doctype, ' ',  1)))
                   from dms_report
                   LIMIT 1);
select my_variable;

The declare itself looks okay, assuming this code is inside a programming block.

EDIT:

If the code is not inside a programming block (such as a stored procedure), then I would just do:

 SELECT @var := Quote(lower(substring_index(doctype, ' ',  1)))
 FROM dms_report
 LIMIT 1;

SELECT @var;