在具有动态sql的plsql函数中处理空值

时间:2014-05-12 09:53:37

标签: oracle plsql

我正在使用一个函数,因为我使用的是动态sql。目前我处理的不是空值,但现在我需要为所有rec.FB_LVL处理空值。 在下面的代码中,我需要使用"如果rec.FB_LVL *为空那么"。任何人都可以告诉我如何在我的下面的函数中使用它。

FUNCTION test_sample(
    p_num NUMBER,

  ) RETURN CLOB IS

  query_str CLOB;
  qry CLOB;

  BEGIN

    FOR rec IN (SELECT * FROM item_details WHERE data_source ='INERTNAL' AND is_active = 'Y'
                   )
                   )
    LOOP
        qry :='
              OR (1=1';

        IF rec.FB_LVL2 IS NOT NULL THEN
            IF rec.FB_LVL2_FLAG = 'I' THEN
            qry := qry||'
                  AND FB_LVL2 IN ('||rec.FB_LVL2||')';
            ELSE
            qry := qry||'
                  AND FB_LVL2 NOT IN ('||rec.FB_LVL2||')';
            END IF;
        END IF;

        IF rec.FB_LVL3 IS NOT NULL THEN
            IF rec.FB_LVL3_FLAG = 'I' THEN
            qry := qry||'
                  AND FB_LVL3 IN ('||rec.FB_LVL3||')';
            ELSE
            qry := qry||'
                  AND FB_LVL3 NOT IN ('||rec.FB_LVL3||')';
            END IF;
        END IF;

        IF rec.FB_LVL4 IS NOT NULL THEN
            IF rec.FB_LVL4_FLAG = 'I' THEN
            qry := qry||'
                  AND FB_LVL4 IN ('||rec.FB_LVL4||')';
            ELSE
            qry := qry||'
                  AND FB_LVL4 NOT IN ('||rec.FB_LVL4||')';
            END IF;
        END IF;

        IF rec.FB_LVL5 IS NOT NULL THEN
            IF rec.FB_LVL5_FLAG = 'I' THEN
            qry := qry||'
                  AND FB_LVL5 IN ('||rec.FB_LVL5||')';
            ELSE
            qry := qry||'
                  AND FB_LVL5 NOT IN ('||rec.FB_LVL5||')';
            END IF;
        END IF;

        IF rec.FB_LVL6 IS NOT NULL THEN
            IF rec.FB_LVL6_FLAG = 'I' THEN
            qry := qry||'
                  AND FB_LVL6 IN ('||rec.FB_LVL6||')';
            ELSE
            qry := qry||'
                  AND FB_LVL6 NOT IN ('||rec.FB_LVL6||')';
            END IF;
        END IF;

        IF rec.FB_LVL7 IS NOT NULL THEN
            IF rec.FB_LVL7_FLAG = 'I' THEN
            qry := qry||'
                  AND FB_LVL7 IN ('||rec.FB_LVL7||')';
            ELSE
            qry := qry||'
                  AND FB_LVL7 NOT IN ('||rec.FB_LVL7||')';
            END IF;
        END IF;

        IF rec.FB_LVL8 IS NOT NULL THEN
            IF rec.FB_LVL8_FLAG = 'I' THEN
            qry := qry||'
                  AND FB_LVL8 IN ('||rec.FB_LVL8||')';
            ELSE
            qry := qry||'
                  AND FB_LVL8 NOT IN ('||rec.FB_LVL8||')';
            END IF;
        END IF;

        IF rec.FB_LVL9 IS NOT NULL THEN
            IF rec.FB_LVL9_FLAG = 'I' THEN
            qry := qry||'
                  AND FB_LVL9 IN ('||rec.FB_LVL9||')';
            ELSE
            qry := qry||'
                  AND FB_LVL9 NOT IN ('||rec.FB_LVL9||')';
            END IF;
        END IF;

        IF rec.FB_LVL10 IS NOT NULL THEN
            IF rec.FB_LVL10_FLAG = 'I' THEN
            qry := qry||'
                  AND FB_LVL10 IN ('||rec.FB_LVL10||')';
            ELSE
            qry := qry||'
                  AND FB_LVL10 NOT IN ('||rec.FB_LVL10||')';
            END IF;
        END IF;

        IF rec.LV_PROD_TYPE_0_CODE IS NOT NULL THEN
            qry := qry||'
                  AND PROD_TYPE_0_CODE IN ('||rec.LV_PROD_TYPE_0_CODE||')';
        END IF;

        IF rec.LV_PROD_TYPE_2_CODE IS NOT NULL THEN
            qry := qry||'
                  AND PROD_TYPE_2_CODE IN ('||rec.LV_PROD_TYPE_2_CODE||')';
        END IF;

        qry:= qry||'
                 )';
        query_str := query_str||qry;

    END LOOP;

    RETURN query_str;

  END test_sample; 

非常感谢先进..

0 个答案:

没有答案