Search code examples
oracle-databaseoracle10gjdeveloper

How to execute one of the two queries based on a Condition in Oracle 10g?


I have to run one of the two queries through a ViewObject in Oracle Adf. It's based on a condition that I may or may not enter a group Code. The first query handles the scenario when the user will not input any parameters and the second query handles the scenario when user inputs a Group Code. depending on whether user inputs parameter or not, I have to run on the following queries. I will be using only one View Object.

1.SELECT IIM.index_num ,IIM.description FROM inv_item_mst IIM WHERE IIM.group_cd IN (:GroupCode1,:GroupCode2,:GroupCode3,:GroupCode4,:GroupCode5,:GroupCode6,:GroupCode7) AND IIM.generic_cd LIKE NVL(:generic_cd_param,'%') AND IIM.supplier_cd LIKE NVL(:supplier_cd_param,'%')

2.SELECT IIM.index_num ,IIM.description FROM inv_item_mst IIM WHERE IIM.group_cd =:groupCd


Solution

  • Since you are selecting the same columns from the same table just combine the statements with an OR condition in the WHERE clause checking if the :groupCd value is present:

    SELECT IIM.index_num, IIM.description
    FROM inv_item_mst IIM
    WHERE (:groupCd IS NOT NULL AND IIM.group_cd =:groupCd)
    OR (:groupCd IS NULL AND
        IIM.group_cd IN (:GroupCode1,:GroupCode2,:GroupCode3,:GroupCode4,:GroupCode5,:GroupCode6,:GroupCode7)
        AND IIM.generic_cd LIKE NVL(:generic_cd_param,'%')
        AND IIM.supplier_cd LIKE NVL(:supplier_cd_param,'%')
       )