簡體   English   中英

我可以在plsql case語句then子句中執行條件嗎?

[英]Can I execute a conditional inside of a plsql case statement then clause?

我正在構建一個腳本,當前復制PHP腳本中的邏輯,我來到這一部分:

 switch ($_POST['type']) {
                    case 'person':
                        $type = '3';

                        if ($_POST['resident']) {

                            $type = '1';

                        }

我需要在我的plsql腳本中復制它。

在我的plsql腳本中,我目前有一個處理$ type設置的case語句。

CASE v_type
    WHEN 'person'
        THEN v_application_type := '3';

我想做點什么

CASE v_type
    WHEN 'person'
        THEN v_application_type := '3';
        AND IF(v_resident = 'Y') THEN v_application_type :='1';

如何在這樣的開關中嵌入條件?

就像是:

case v_type
    when 'person' then
        v_application_type := '3';

        if v_resident = 'Y' then
            v_application_type :='1';
        end if;
    else
        v_application_type := 'cheese';
end case;

雖然在if條件中引入默認的'3'可能更簡潔:

case v_type
    when 'person' then
        if v_resident = 'Y' then
            v_application_type := '1';
        else
            v_application_type := '3';
        end if;
    else
        v_application_type := 'cheese';
end case;

或者你可以堅持使用case ,因為他們做類似的工作,中途切換風格可能會令人困惑:

case v_type
    when 'person' then
        case v_resident
            when 'Y' then
                v_application_type := '1';
            else
                v_application_type := '3';
        end case;
    else
        v_application_type := 'cheese';
end case;

但是,如果它只是分配一個值,我會把整個事情作為一個表達式:

v_application_type :=
    case v_type
        when 'person' then                
            case v_resident
                when 'Y' then '1'
                else '3'
            end
        else 'cheese'
    end;

(順便說一下,如果v_application_type是一個數字,你就不會引用它。)

如果刪除AND並添加END IF ,則可以執行此操作; 更清晰地對齊:

CASE v_type
    WHEN 'person' THEN
        v_application_type := '3';
        IF v_resident = 'Y' THEN
            v_application_type :='1';
        END IF;
    ...
END CASE;

或更改案例搜索,這將涉及重復的事情,但可能更清楚:

CASE WHEN v_type = 'person' AND v_resident = 'Y' THEN
         v_application_type :='1';
     WHEN v_type = 'person' THEN
         v_application_type := '3';
     ...
END CASE;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM