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
答案 0 :(得分:1)
你可以试试这个:
registration_type_id
问题(S)
将DROP PROCEDURE IF EXISTS `uspAddUser`;
DELIMITER //
CREATE PROCEDURE `uspAddUser` (username VARCHAR(50), email VARCHAR(50), password VARCHAR(50), avatar VARCHAR(50))
BEGIN
DECLARE `userID` BIGINT(20);
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
END;
START TRANSACTION;
-- Get the next highest ID and lock the table until the end of the transaction
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//
DELIMITER ;
的表格设置为自动增量列吗?如果没有,请做..因为它将消除部分:
ID