简体   繁体   English

创建MySQL存储过程的问题

[英]Problems to create MySQL Stored Procedure

I am trying to create this procedure: 我正在尝试创建此过程:

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 ;

I am using this table: 我正在使用此表:

 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 )
  )

And I am receiving this error message: 而且我收到此错误消息:

#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

This is the first time I create an Stored Procedure with this kind of complexity and I don't understand why this error is appearing. 这是我第一次创建具有这种复杂性的存储过程,但我不明白为什么会出现此错误。

Thank you in advance for any help! 预先感谢您的任何帮助!

Declarations should immediately follow the begin block. 声明应立即放在begin块之后。 So just move the declaration up to the beginning of the function: 因此,只需将声明移至函数的开头即可:

BEGIN
    DECLARE exist INT;
    DECLARE active INT;

    . . .

By the way, exist is a very bad name for a variable because it is very similr to a MySQL reserved word. 顺便说一句, exist是一个非常不好的变量名,因为它与MySQL保留字非常相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM