在存储过程中使用case-when语句语法

时间:2019-03-01 07:24:55

标签: sql stored-procedures syntax switch-statement mariadb

IF....ELSE IF...ELSE更改为SWITCH之后。我收到错误代码:1064

代替IF....ELSE IF...ELSE语句为两个以上的条件执行不同的代码,而是更喜欢上面我更改为的代码 SWITCH语句根据不同的条件执行一个动作,然后从break条件中退出SWITCH,而不是多次执行IF条件。

尤其是CASE WHEN FeedFlagStatus == '5' THEN

  

错误代码:1064

     

您的SQL语法有错误;检查手册   对应于您的MariaDB服务器版本,以使用正确的语法   在'= 5附近                             更新feed_item_setting设置read_status =第23行的ReadStatus WHERE'

DELIMITER @@;

CREATE 
    PROCEDURE `saveProfileSetting`(IN `UserId` BIGINT, IN `FeedFlagStatus` INT, IN `ReadStatus` INT, IN `WriteStatus` INT, IN `WriteCmtStatus` INT) 
    NOT DETERMINISTIC 
    CONTAINS SQL 
    SQL SECURITY DEFINER 
    BEGIN
        DECLARE userid VARCHAR(255);
        DECLARE noOfHits INT(11);
        DECLARE noOfHits2 INT(11);
        DECLARE intCode INT(11);

        SET userid = UserId;

        SET noOfHits = ( SELECT `user_id` FROM `feed_item_setting` WHERE `user_id` = UserId );
        SET noOfHits2 = ( SELECT COUNT(*) FROM `feed_item_setting` WHERE `user_id` = UserId );

            IF EXISTS (SELECT `user_id` FROM `feed_item_setting` WHERE `user_id` = UserId ) THEN  
                BEGIN

                    CASE  
                        WHEN FeedFlagStatus == 5 THEN
                            UPDATE `feed_item_setting` SET `read_status` = ReadStatus WHERE `user_id` = UserId;
                            SET intCode = FeedFlagStatus;
                        WHEN FeedFlagStatus == 6 THEN
                            UPDATE `feed_item_setting` SET `write_status` = WriteStatus WHERE `user_id` = UserId;
                            SET intCode = FeedFlagStatus;
                        WHEN FeedFlagStatus == 7 THEN
                            UPDATE `feed_item_setting` SET `write_cmt_status` = WriteCmtStatus WHERE `user_id` = UserId;
                            SET intCode = FeedFlagStatus;
                    END CASE;
                END;            
            ELSE
                BEGIN
                    CASE  
                        WHEN FeedFlagStatus == 5 THEN
                            INSERT INTO `feed_item_setting` (`user_id`, `read_status`) VALUES (UserId, ReadStatus);
                            SET intCode = FeedFlagStatus;
                        WHEN FeedFlagStatus == 6 THEN
                            INSERT INTO `feed_item_setting` (`user_id`, `write_status`) VALUES (UserId, WriteStatus);
                            SET intCode = FeedFlagStatus;
                        WHEN FeedFlagStatus == 7 THEN
                            INSERT INTO `feed_item_setting` (`user_id`, `write_cmt_status`) VALUES (UserId, WriteCmtStatus);
                            SET intCode = FeedFlagStatus;
                    END CASE;
                END;
            END IF;



        SELECT userid, noOfHits, noOfHits2, intCode;
    END;

@@;  -- Altered delimiter terminated compound statement

DELIMITER ; -- restore delimiter to standard semicolon

1 个答案:

答案 0 :(得分:0)

自70年代初期由IBM开发以来,它一直使用一个等号进行比较。它最初被命名为“结构化英语查询语言”,因此它旨在与普通人类语言接近。

分配给会话变量用:=表示,并且可以通过以下方式完成分配给变量

DECLARE locRecord VARCHAR(255);
SELECT * INTO locRecord FROM `location_map` WHERE `user_id` = UserId;

DELIMITER @@;

CREATE 
    PROCEDURE `saveProfileSetting`(IN `UserId` BIGINT, IN `FeedFlagStatus` INT, IN `ReadStatus` INT, IN `WriteStatus` INT, IN `WriteCmtStatus` INT) 
    NOT DETERMINISTIC 
    CONTAINS SQL 
    SQL SECURITY DEFINER 
    BEGIN
        DECLARE userid VARCHAR(255);
        DECLARE noOfHits INT(11);
        DECLARE noOfHits2 INT(11);
        DECLARE intCode INT(11);

        SET userid = UserId;

        SET noOfHits = ( SELECT `user_id` FROM `feed_item_setting` WHERE `user_id` = UserId );
        SET noOfHits2 = ( SELECT COUNT(*) FROM `feed_item_setting` WHERE `user_id` = UserId );
        -- IF ( SELECT count(*) FROM `group_users` WHERE `group_id` = groupId AND `userid` = userId AND `stataus` = 1 ) > 0 THEN


            IF EXISTS (SELECT `user_id` FROM `feed_item_setting` WHERE `user_id` = UserId ) THEN  
                BEGIN

                    CASE  
                        WHEN FeedFlagStatus = 5 THEN
                            UPDATE `feed_item_setting` SET `read_status` = ReadStatus WHERE `user_id` = UserId;
                            SET intCode:= FeedFlagStatus;
                        WHEN FeedFlagStatus = 6 THEN
                            UPDATE `feed_item_setting` SET `write_status` = WriteStatus WHERE `user_id` = UserId;
                            SET intCode:= FeedFlagStatus;
                        WHEN FeedFlagStatus = 7 THEN
                            UPDATE `feed_item_setting` SET `write_cmt_status` = WriteCmtStatus WHERE `user_id` = UserId;
                            SET intCode:= FeedFlagStatus;
                    END CASE;
                END;            
            ELSE
                BEGIN
                    CASE  
                        WHEN FeedFlagStatus = 5 THEN
                            INSERT INTO `feed_item_setting` (`user_id`, `read_status`) VALUES (UserId, ReadStatus);
                            SET intCode:= FeedFlagStatus;
                        WHEN FeedFlagStatus = 6 THEN
                            INSERT INTO `feed_item_setting` (`user_id`, `write_status`) VALUES (UserId, WriteStatus);
                            SET intCode:= FeedFlagStatus;
                        WHEN FeedFlagStatus = 7 THEN
                            INSERT INTO `feed_item_setting` (`user_id`, `write_cmt_status`) VALUES (UserId, WriteCmtStatus);
                            SET intCode:= FeedFlagStatus;
                    END CASE;
                END;
            END IF;



        SELECT userid, noOfHits, noOfHits2, intCode;
    END;

@@;  -- Altered delimiter terminated compound statement

DELIMITER ; -- restore delimiter to standard semicolon

历史重演给那些不注意的人,学习注定要再次重演。不是很明显吗?