Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

CASE statement to create dynamic WHERE clause

2652054Feb 28 2019 — edited Mar 1 2019

Hi Experts,

I have a question and I don't know if this is possible or not , I am trying to use the CASE statement in WHERE clause to create the SQL

I have an input parameter p_org , If the value of

p_org is 'ABC' , then org_id in (123)

p_org is 'DEF' , then org_id in (456)

p_org is 'GHI' , then org_id in (789)

IF p_org value is ALL , then the query should run for all the three org_id in (123,456,789)

-- i mean something like this but i am unable to figure out how

select * from all_org_tbl

where 1=1

and (

case when

                p_org = 'ALL' THEN org_id in in (123,456,789)

             when

                 p_org = 'ABC' THEN org_id in (123)

             when

                p_org = 'DEF' THEN org_id in (456)

            WHEN

                 p_org = 'GHI' THEN org_id in (789)

            END

)

is it possible to achieve this with CASE statement, any help is much appreciated.

This post has been answered by Cookiemonster76 on Feb 28 2019
Jump to Answer
Comments
Post Details
Added on Feb 28 2019
8 comments
2,184 views