简体   繁体   中英

Using case-when statement in stored procedure Syntax

After changing from IF....ELSE IF...ELSE to SWITCH . I'm getting Error Code: 1064

Instead of IF....ELSE IF...ELSE statement executes different codes for more than two conditions, rather preferring to the above I had changed to SWITCH statement to perform one single actions based on different conditions and later to break out of SWITCH condition rather executing IF conditions several times.

Especially with CASE WHEN FeedFlagStatus == '5' THEN

Error Code: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= 5 THEN UPDATE feed_item_setting SET read_status = ReadStatus WHERE' at line 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

It has used a single equals sign for comparison since the early '70s developed by IBM It was originally named Structured English Query Language, so it's intended to be close to regular human language.

Assignment to a session variable is denoted by := and assigning to variable can be done in the following way

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

History repeats itself for those Who Do Not Pay Attention and Learn were Doomed To Repeat It again. Isn't it obvious?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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