我正在尝试创建此过程:
DELIMITER //
CREATE PROCEDURE PA_INCLUDE_SETTING
(IN SettingType INT(10),
IN SettingName INT(10),
IN Active INT(1),
OUT result INT(1))
BEGIN
DECLARE exist INT;
SELECT COUNT(*) INTO exist
FROM tbSetting
WHERE nmSetting = SettingName
AND cdSettingType = SettingType;
IF (exist > 0) THEN
DECLARE active INT;
SELECT cdActive INTO active
FROM tbSetting
WHERE nmSetting = SettingName
AND cdTypeSetting = SettingType;
IF (active = 0) THEN
UPDATE tbSetting
SET cdActive = 1
WHERE nmSetting = SettingName
AND cdSettingType = SettingType;
SET result = -1;
ELSE
SET result = -2;
END IF;
ELSE
INSERT INTO tbSetting
(cdSettingType, nmSetting, cdActive)
VALUES (SettingType, Setting, Active);
SET result = 0;
END IF;
END //
DELIMITER ;
我正在使用此表:
CREATE TABLE tbSetting
(
cdSettingType INT(10) NOT NULL,
cdSetting INT(10) IDENTITY(1,1) NOT NULL,
nmSetting VARCHAR(30),
cdActive INT(1),
CONSTRAINT pk_cdSetting PRIMARY KEY (cdSettingType, cdSetting )
)
我收到此错误消息:
#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 'DECLARE active INT;
SELECT cdActive
FROM tbSetting
WHERE n' at line 13
这是我第一次创建具有这种复杂性的存储过程,但我不明白为什么会出现此错误。
提前感谢您的帮助!
答案 0 :(得分:1)
声明应紧跟begin
块。所以只需将声明移到函数的开头:
BEGIN
DECLARE exist INT;
DECLARE active INT;
. . .
顺便说一下,exist
是变量的一个非常糟糕的名称,因为它与MySQL保留字非常相似。