2

It seems like this must be a dumb question :) but I can't seem to find it posted any where.

I need to set a number of local variables in a sproc from a select statement.

This works from the query window:

select  @value1 := value1, @value2 := value2 from test_table limit 1; 
select  @value1, @value2;

But when I try and do a similar thing in a stored procedure, I get: SQL Error (1064):You have an error in your SQL syntax...

BEGIN

    declare p_value1 varchar(50); 
    declare p_value2 varchar(50); 
    select  p_value1 := value1, p_value2 := value2 from test_table limit 1; 

END

Any help appreciated.

3
  • Did you include a DELIMITER $$ (or similar) statement before your procedure? Commented Feb 2, 2019 at 3:32
  • In SQL, isn’t assignment just a regular = sign (no : symbol) Commented Feb 2, 2019 at 3:41
  • Yes - it wasn't an issue with declaring the sproc. The answer below did the trick. BTW the := does work from the query window. I'm used to SQL Server - what works in the query window works in a sproc but not true with MySQL. Commented Feb 2, 2019 at 4:41

1 Answer 1

4

Use the SELECT...INTO syntax:

BEGIN

    declare p_value1 varchar(50); 
    declare p_value2 varchar(50); 
    select value1, value2 into p_value1, p_value2 from test_table limit 1; 

END
Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks! I'm used to SQL Server - this is a bit different.
Yeah, MySQL is totally different. You really need to make yourself at home in their reference documentation. I've been using MySQL for many years, and I open their doc daily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.