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
  • 37,778
  • 11
  • 85
  • 87
Ahmed Mozaly
  • 1,454
  • 2
  • 15
  • 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,537
  • 3
  • 26
  • 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
  • 50,714
  • 13
  • 121
  • 117
0

We can't use Declare in normal queries.

Try this:

SET @ID =(Select id from mytable)
Manuel
  • 3,828
  • 6
  • 33
  • 48
Muhammad Rashid
  • 563
  • 1
  • 6
  • 25