1
BEGIN
    CASE x
        WHEN 0 THEN (
                    IF thisIsAVariable = 'viewAll' THEN
                       SELECT id, fname FROM Persons WHERE 1
                    ELSE
                       SELECT id, fname FROM Persons WHERE id = '1111'
                    END IF
                 );
    END CASE;
END

casewhen 语句中 ifelse 语句的正确语法是什么?

4

3 回答 3

1

只需使用IF-ELSE

DELIMITER $$
CREATE PROCEDURE procName
BEGIN
    IF x = 0 THEN
        IF thisIsAVariable = 'viewAll' THEN
            SELECT id, fname FROM Persons WHERE 1;
        ELSE
            SELECT id, fname FROM Persons WHERE id = '1111';
        END IF;
    ELSE
        -- other codes if you have
    END IF;
END $$
DELIMITER ;

我认为在使用时CASE,select 语句应该只返回一个值。

于 2013-02-15T06:05:59.100 回答
1

我们可以在 if 中使用 if 来简化代码,如下所示:

BEGIN
    IF x = 0 THEN
        IF thisIsAVariable = 'viewAll' THEN
            SELECT id, fname FROM Persons WHERE 1;
        ELSE
            SELECT id, fname FROM Persons WHERE id = '1111';
        END IF;
    END IF;
END 
于 2013-02-15T06:17:05.197 回答
0

试试这个

BEGIN
    CASE x
        WHEN 0 THEN (
                    SELECT id, fname FROM Persons WHERE id = IF(thisIsAVariable = 'viewAll',1,'1111')
                 );
    END CASE;
END
于 2013-02-15T07:03:04.763 回答