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 :)
Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.
The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.
MySQL allows you to declare two or more variables that share the same data type using a single DECLARE statement. The following example declares two integer variables x and y , and set their default values to zero.
In SQL Server, symbol @@ is prefixed to global variables. The server maintains all the global variables.
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.
DECLARE @count INT;
SET @count = 5633;
SELECT count(matchid)
FROM matches
WHERE id = @count;
Also, you apparently need a begin/end block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With