我已经在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行有什么问题?
致谢
答案 0 :(得分:1)
$$
),而不是({;
)。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 ;