SuprDewd
SuprDewd

Reputation: 269

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?

Upvotes: 6

Views: 2876

Answers (1)

The Scrum Meister
The Scrum Meister

Reputation: 30111

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.

Upvotes: 6

Related Questions