【发布时间】:2018-09-01 14:26:23
【问题描述】:
我正在尝试将我过去编写的 SQL 存储过程转换为 MySQL。这个错误给我带来了麻烦。
我正在使用 phpmyadmin 4.7.4 创建这个程序
我得到的错误是 SET userID = SELECT MAX(ID) + 1 FROM users 我还在代码中在它之前放置了一个标签,所以你们更容易找到。
输出的错误是:
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;//
如果你们想看的话,这是原始的 SQL 查询
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
【问题讨论】:
标签: mysql stored-procedures phpmyadmin