5
DECLARE @ID INT ;

This statement parses fine with MS SQL Server, but gives me

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 'DECLARE @ID INT' at line 1

Does anyone have any idea about the reason?

Samuel
  • 36,810
  • 11
  • 84
  • 86
Ahmed Mozaly
  • 1,344
  • 2
  • 14
  • 22

3 Answers3

9

DECLARE is used in stored procedures/functions.

If you're looking to set a variable for just regular queries, use SET

Darryl E. Clarke
  • 7,417
  • 3
  • 25
  • 34
  • -1 @ID Doesn't seem to work for me on MySQL 5.0 even inside procedures/functions. –  Feb 12 '13 at 20:02
  • -1 for answering the question? Works perfectly well as documented here: http://dev.mysql.com/doc/refman/5.0/en/declare.html – Darryl E. Clarke Feb 12 '13 at 22:31
  • You only answered half of it. When I try `DECLARE @ID INT ;` in a procedure I get: "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 '@ID INT ; END' at line 2". –  Feb 13 '13 at 15:39
3

try naming the variable without the @

DECLARE id INT;
Jose Basilio
  • 49,569
  • 12
  • 117
  • 116
0

We can't use Declare in normal queries.

Try this:

SET @ID =(Select id from mytable)
Manuel
  • 3,638
  • 6
  • 32
  • 46
Muhammad Rashid
  • 551
  • 1
  • 6
  • 24