在MySQL中创建存储过程时遇到问题

时间:2018-09-23 09:06:59

标签: mysql stored-procedures mysql-error-1064

我已经在MySQL中编写了以下脚本来创建存储过程:

CREATE PROCEDURE `AddBranch`(
IN `inCompanyCode` char(3), 
IN `inBranchCode` varchar(6), 
IN `inBankBranch` varchar(40)
)
BEGIN
    DECLARE branchExists TINYINT DEFAULT 0;
    SELECT Count(*) INTO branchExists FROM branches WHERE CompanyCode = inCompanyCode AND BranchCode = inBranchCode;
    IF branchExists = 0 THEN
        INSERT INTO branches VALUES (inCompanyCode, inBranchCode, inBankBranch);
    ELSE
        UPDATE branches SET Branch = inBankBranch
        WHERE CompanyCode = inCompanyCode AND BranchCode = inBranchCode;
    END IF;
END;

在运行查询时,显示的错误消息是:

1064-您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在第7行的“''附近使用

我什至尝试过DECLARE branchExists TINYINT(1) DEFAULT 0;,但问题仍然存在。

第7行有什么问题?

致谢

1 个答案:

答案 0 :(得分:1)

  • 您需要将Delimiter重新定义为其他名称(例如:$$),而不是({;)。
  • 作为安全措施,请检查是否已经存在相同的命名过程(DROP PROCEDURE IF EXISTS
  • 最后,将DELIMITER重新定义为;

将存储过程更改如下:

DELIMITER $$
DROP PROCEDURE IF EXISTS `AddBranch`$$

CREATE PROCEDURE `AddBranch`(
IN `inCompanyCode` char(3), 
IN `inBranchCode` varchar(6), 
IN `inBankBranch` varchar(40)
)
BEGIN
    DECLARE branchExists TINYINT DEFAULT 0;
    SELECT Count(*) INTO branchExists FROM branches WHERE CompanyCode = inCompanyCode AND BranchCode = inBranchCode;
    IF branchExists = 0 THEN
        INSERT INTO branches VALUES (inCompanyCode, inBranchCode, inBankBranch);
    ELSE
        UPDATE branches SET Branch = inBankBranch
        WHERE CompanyCode = inCompanyCode AND BranchCode = inBranchCode;
    END IF;
END$$

DELIMITER ;