Creating dynamic SQL need little trick and multiple steps. Main idea is to generate a sql based on inputs dynamically. If user selects any parameter(s) then only the 'WHERE' be active else it will be inactive.
This performs efficiently when you have complex SQL with large tables and you want to avoid IN('All')
kind of default clauses. This is a three step process, explained below.
Works only for Oracle DB.
Step A. create LOVs -
Concat -
SELECT ''''||ABCD||'''' FROM (
SELECT LISTAGG( COLUMN_VALUE,''',''') WITHIN GROUP (ORDER BY 1) as ABCD
FROM TABLE(sys.ODCIVARCHAR2LIST(:prmTerritory)))
Break -
Select ' AND 1=1' from dual where :prmTerritoryConcat='''''' OR :prmTerritoryConcat = '''All'''
union all
select ' AND orig_table.Territory IN ('|| :prmTerritoryConcat ||')' from dual where :prmTerritoryConcat <> '''''' AND :prmTerritoryConcat <> '''All'''
Normal LOV for Territory -
Select Territory from master_Territory
Step B.0 Create below parameters - please note how we are using these parameters in SQL. Display Parameters-
- parTerritory - set to #3
Hidden Parameters-
- parTerritoryBreak - set to #2
- parTerritoryConcat - set to #1
Step B.1 Add all these parameters to Source Database into a procedure.
Step C. create the SQL using above parameters -
Query:
SELECT Territory
from original_table original_table
WHERE
&parTerritoryBreak
Test cases - I considered two cases below. Check the output log for the sql BIP is creating. Log should show like this for both cases -
Case 1 - when territory isn't selected -
SELECT Territory
from original_table original_table
WHERE
1=1
Case 2 - when territories are selected -
SELECT Territory
from original_table original_table
WHERE 1=1
AND orig_table.Territory IN ('Americas', 'EMEA', 'APAC')