95

I´d like to SELECT a single value into a variable. I´d tried to following:

DECLARE myvar INT(4);

-- immediately returns some syntax error.

SELECT myvalue 
  FROM mytable 
 WHERE anothervalue = 1;

-- returns a single integer

SELECT myvalue 
  INTO myvar 
  FROM mytable 
 WHERE anothervalue = 1;

-- does not work, also tried @myvar

Is possible to use DECLARE outside of stored procedures or functions?

Maybe I just dont get the concept of user variables... I just tried:

SELECT myvalue INTO @var FROM `mytable` WHERE uid = 1;
SELECT @var;

...which worked just like it´s supposed to. But if I run each query at a time i just get @var NULL.

Daniel Fischer
  • 174,737
  • 16
  • 293
  • 422
Matt Bannert
  • 25,237
  • 34
  • 134
  • 195

11 Answers11

112

I ran into this same issue, but I think I know what's causing the confusion. If you use MySql Query Analyzer, you can do this just fine:

SELECT myvalue 
INTO @myvar 
FROM mytable 
WHERE anothervalue = 1;

However, if you put that same query in MySql Workbench, it will throw a syntax error. I don't know why they would be different, but they are. To work around the problem in MySql Workbench, you can rewrite the query like this:

SELECT @myvar:=myvalue
FROM mytable
WHERE anothervalue = 1;
Tim Gautier
  • 25,415
  • 4
  • 42
  • 49
  • 6
    Indeed that is interesting. Added a MySQL workbench tag to the question – so others might find that this issue is related to MySQL Workbench. – Matt Bannert Sep 08 '11 at 07:43
  • 1
    I used MySQL workbench version 5.2.47 rev 10398 on Fedora 18 and there is not such problem with it. – GoYun.Info Apr 18 '13 at 16:04
  • the second solution `:=` will be deprecated. So prefer to use the `SELECT ... INTO` ! – Meloman Sep 14 '20 at 11:28
45

In the end a stored procedure was the solution for my problem. Here´s what helped:

DELIMITER //
CREATE PROCEDURE test ()
  BEGIN
  DECLARE myvar DOUBLE;
  SELECT somevalue INTO myvar FROM mytable WHERE uid=1;
  SELECT myvar;
  END
  //

DELIMITER ;

call test ();
Barkermn01
  • 6,295
  • 30
  • 71
Matt Bannert
  • 25,237
  • 34
  • 134
  • 195
  • Thx for the testing... sounds interesting. Unfortunately I do not get the last sentence. Did you forget a word or two? – Matt Bannert Nov 23 '10 at 09:09
42

These answers don't cover very well MULTIPLE variables.

Doing the inline assignment in a stored procedure causes those results to ALSO be sent back in the resultset. That can be confusing. To using the SELECT...INTO syntax with multiple variables you do:

SELECT a, b INTO @a, @b FROM mytable LIMIT 1;

The SELECT must return only 1 row, hence LIMIT 1, although that isn't always necessary.

Garr Godfrey
  • 6,040
  • 2
  • 18
  • 20
22

You can also use SET instead of DECLARE

SET @myvar := (SELECT somevalue INTO myvar FROM mytable WHERE uid=1);

SELECT myvar;
Somwang Souksavatd
  • 3,974
  • 26
  • 28
15

Per the MySQL docs DECLARE works only at the start of a BEGIN...END block as in a stored program.

Dan U.
  • 1,317
  • 3
  • 15
  • 19
  • After googling for 'mysql select inside procedure' and arrived here, not having the declarations at the beginning is what was causing the `syntax error, missing ;` error for me. – Miguel Pynto Sep 25 '18 at 11:06
12

You don't need to DECLARE a variable in MySQL. A variable's type is determined automatically when it is first assigned a value. Its type can be one of: integer, decimal, floating-point, binary or nonbinary string, or NULL value. See the User-Defined Variables documentation for more information:

http://dev.mysql.com/doc/refman/5.0/en/user-variables.html

You can use SELECT ... INTO to assign columns to a variable:

http://dev.mysql.com/doc/refman/5.0/en/select-into-statement.html

Example:

mysql> SELECT 1 INTO @var;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT @var;
+------+
| @var |
+------+
| 1    |
+------+
1 row in set (0.00 sec)
Mike
  • 20,127
  • 2
  • 38
  • 65
  • 1
    Hmm, somehow I am in trouble here.. SELECT 1 INTO @var; returns a syntax error too. so does: SELECT somevar INTO @var FROM mytable; Maybe it´s a DELIMITER problem? – Matt Bannert Jun 19 '10 at 11:09
  • What error are you receiving? What version of MySQL are you using? – Mike Jun 19 '10 at 11:26
  • Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO @var' at line 3. Version=5.5.16 – Scott Willeke Sep 29 '11 at 23:15
4

It is worth noting that despite the fact that you can SELECT INTO global variables like:

SELECT ... INTO @XYZ ...

You can NOT use FETCH INTO global variables like:

FETCH ... INTO @XYZ

Looks like it's not a bug. I hope it will be helpful to someone...

b.b3rn4rd
  • 7,431
  • 2
  • 37
  • 52
4

I am using version 6 (MySQL Workbench Community (GPL) for Windows version 6.0.9 revision 11421 build 1170) on Windows Vista. I have no problem with the following options. Probably they fixed it since these guys got the problems three years ago.

/* first option */
SELECT ID 
INTO @myvar 
FROM party 
WHERE Type = 'individual';

-- get the result
select @myvar;

/* second option */
SELECT @myvar:=ID
FROM party
WHERE Type = 'individual';


/* third option. The same as SQL Server does */
SELECT @myvar = ID FROM party WHERE Type = 'individual';

All option above give me a correct result.

Tman
  • 51
  • 1
3

For those running in such issues right now, just try to put an alias for the table, this should the trick, e.g:

SELECT myvalue 
  INTO myvar 
  FROM mytable x
 WHERE x.anothervalue = 1;

It worked for me.

Cheers.

Derlin
  • 8,518
  • 2
  • 22
  • 42
emmanuel
  • 31
  • 1
1

You maybe miss the @ symbol before your value,like that select 'test' INTO @myValue;

HiMan
  • 11
  • 2
0

SELECT c1, c2, c3, ... INTO @v1, @v2, @v3,... FROM table_name WHERE condition;