存储过程:if else语句里面的情况

时间:2013-02-15 06:04:29

标签: mysql sql

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语句的正确语法是什么?

3 个答案:

答案 0 :(得分: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语句应该只返回一个值。

答案 1 :(得分: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 

答案 2 :(得分:0)

试试这个

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