Oracle APEX-基于页面项的动态where子句

时间:2019-04-18 20:18:46

标签: sql oracle plsql oracle-apex

有没有一种方法可以基于页面项目值构造动态的where子句?

也许使用CASE WHEN吗?

SELECT a.ID, a.NAME
FROM Table1 a
WHERE 
    CASE WHEN mypackage.get_type_id(TO_NUMBER(:P2_TEST_TYPE))='TYPE1' OR 
           mypackage.get_type_id(TO_NUMBER(:P2_TEST_TYPE))='TYPE2' THEN
        NOT EXISTS (
          SELECT 1
          FROM   Table2 b
          WHERE  b.id = a.id            
             AND  b.type_id = :P2_TEST_TYPE
             AND mypackage.get_category_id(b.parent_id) <> mypackage.get_category_id(:P2_PARENT_ID)
             AND b.status = 'NEW'
       )
  WHEN mypackage.get_type_id(TO_NUMBER(:P2_TEST_TYPE))='TYPE3' THEN
       NOT EXISTS (
          SELECT 1
          FROM   Table2 b
          WHERE  b.id = a.id            
             AND  b.type_id = :P2_TEST_TYPE
             AND b.status = 'NEW'
       )
 END 

我该怎么做?

1 个答案:

答案 0 :(得分:1)

也许是这样?

使用两个CASE的并集代替SELECT,但它们每个都有其自己的(附加)条件-您要放入CASE的条件。

SELECT a.ID, a.NAME
FROM Table1 a
WHERE NOT EXISTS
  (SELECT 1
          FROM   Table2 b
          WHERE  b.id = a.id            
             AND b.type_id = :P2_TEST_TYPE
             AND mypackage.get_category_id(b.parent_id) <> mypackage.get_category_id(:P2_PARENT_ID)
             AND b.status = 'NEW'
             AND mypackage.get_type_id(TO_NUMBER(:P2_TEST_TYPE)) IN ('TYPE1', 'TYPE2')
   UNION ALL
   SELECT 1
          FROM   Table2 b
          WHERE  b.id = a.id            
             AND b.type_id = :P2_TEST_TYPE
             AND b.status = 'NEW'
             AND mypackage.get_type_id(TO_NUMBER(:P2_TEST_TYPE)) = 'TYPE3'
  );