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

问题描述 投票:0回答:1

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 SET read_status = ReadStatus WHERE'在第23行

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
sql stored-procedures syntax switch-statement mariadb
1个回答
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

对于那些不注意和学习的人来说,历史会重演,注定要再次重复。不是很明显吗?

© www.soinside.com 2019 - 2024. All rights reserved.