Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Syntax Error In Variable Declaration

I have the following MySQL query:

DELIMITER //
CREATE PROCEDURE InsertResult (IN winnerID INT, IN loserID INT)
BEGIN
    INSERT INTO KomperResult (WinnerID, LoserID) VALUES (@winnerID, @loserID);
    DECLARE winnerScore, loserScore INT;
    SELECT Score INTO @winnerScore FROM KomperPerson WHERE ID = @winnerID;
    SELECT Score INTO @loserScore FROM KomperPerson WHERE ID = @loserID;
    IF (@loserScore >= @winnerScore) THEN UPDATE KomperPerson SET Score = @loserScore + 1 WHERE ID = @winnerID; END IF;
END//

I get an error on:

DECLARE winnerScore, loserScore INT;

What am I doing wrong?

like image 365
SuprDewd Avatar asked Feb 22 '11 20:02

SuprDewd


1 Answers

DECLAREs need to go on the first line of your procedure.

From the docs:

DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

like image 120
The Scrum Meister Avatar answered Sep 20 '22 06:09

The Scrum Meister