I am trying to translate a SQL stored procedure I have written in the past to MySQL. This error is giving me trouble.
I am using phpmyadmin 4.7.4 to create this procedure
The error I am getting is at SET userID = SELECT MAX(ID) + 1 FROM users I have also placed a tag before it in the code so it is easier for you guys to find.
The error that is outputted is:
MySQL said: Documentation
/#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 'SET userID = SELECT MAX(ID) + 1 FROM users;
-- Default to 1 if the table is em' at line 13
CREATE PROCEDURE uspAddUser(username VARCHAR(50), email VARCHAR(50), password VARCHAR(50), avatar VARCHAR(50))
BEGIN
DECLARE userID INTEGER;
BEGIN
ROLLBACK; -- Rollback transaction on error
END;
START TRANSACTION
-- Get the next highest ID and lock the table until the end of the transaction
<ERROR> -> SET userID = SELECT MAX(ID) + 1 FROM users;
-- Default to 1 if the table is empty
SET userID = COALESCE(userID, 1);
-- CREATE new record
INSERT INTO users(userID, username, email, password, avatar)
VALUES(ID, email, password, avatar, 1); -- 1 = Active
-- return ID to calling program
SELECT userID AS ID;
COMMIT;
END;//
This is the original SQL query if you guys want to see that at all
GO
CREATE PROCEDURE uspAddTeam
@strTeam VARCHAR(50)
,@strMascot VARCHAR(50)
AS
SET NOCOUNT ON -- Report Only Errors
SET XACT_ABORT ON -- Rollback transaction on error
BEGIN TRANSACTION
DECLARE @intTeamID INTEGER
-- Get the next highest ID and lock the table until the end of the transaction
SELECT @intTeamID = MAX(intTeamID) + 1 FROM TTeams (TABLOCKX)
-- Default to 1 if the table is empty
SELECT @intTeamID = COALESCE(@intTeamID, 1)
-- CREATE new record
INSERT INTO TTeams(intTeamID, strTeam, strMascot, intTeamStatusID)
VALUES(@intTeamID, @strTeam, @strMascot, 1) -- 1 = Active
-- return ID to calling program
SELECT @intTeamID AS intTeamID
COMMIT TRANSACTION
GO