Cant declare variable SQL

15,226

Solution 1

According to the MySQL manual, DECLARE is only allowed inside a BEGIN ... END block, and must be at the start. You're also forgetting a semicolon on the end of each line. This should work for you:

SET @count = 5633;

SELECT count(*) 
FROM matches
WHERE id = @count;

COUNT(*) is faster in some cases.

Solution 2

DECLARE @count INT;
SET     @count = 5633;

SELECT count(matchid) 
FROM   matches
WHERE   id = @count;

Also, you apparently need a begin/end block.

Share:
15,226
James Stevenson
Author by

James Stevenson

Updated on June 16, 2022

Comments

  • James Stevenson
    James Stevenson almost 2 years

    I've been trying to declare an integer variable but its just not working. Here is my query:

    DECLARE @count INT
    SET     @count = 5633
    
    SELECT count(matchid) 
    FROM   `matches`
    WHERE   id = @count
    

    Im getting this error:

    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 'DECLARE @count INT
    SET     @count = 5633
    

    Please help :)