【问题标题】:Error 1604 Creating MySQL Stored Procedure. MySQL Version 5.7.19错误 1604 创建 MySQL 存储过程。 MySQL 版本 5.7.19
【发布时间】: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


    【解决方案1】:

    你可以试试这个:

    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 的表是否设置为自增列?如果没有,请做..因为它会消除部分:

      -- Get the next highest ID and lock the table until the end of the transaction
      SET userID = (SELECT MAX(ID) + 1 FROM users);
      

    【讨论】:

    • 你是绝对正确的,我只是复制并粘贴存储过程,但是我完全不需要合并行。
    • 我运行了您的查询并收到此错误。 #1337 - 游标或处理程序声明后的变量或条件声明。错误引用了上面所有代码,包括这一行 -> -- 获取下一个最高 ID 并锁定表直到事务结束
    • 我刚刚尝试移动这一行 DECLARE userID INTEGER;到下面开始交易,什么都没有。
    猜你喜欢
    • 2014-05-07
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 2014-03-07
    • 2016-11-11
    • 2017-10-14
    • 2021-07-10
    • 2015-08-27
    相关资源
    最近更新 更多