Pergunta

I am using the oracle 10g and i have table with column names firstname , lastname , country and region .

country | region
--------|----------
India   | Asia
China   | Asia
Ireland | Europe
England | Europe

Below are the senarios,

  1. when we pass "ALL" for country and region , output query should be

    select * from Test_Table
    
  2. when we pass "India,China" for counrty and "Asia , Europe" for region , output query should be

    select * from Test_Table where counrty in (Malayasia,japan) and region in (Asia , Europe)
    

how do i combine this into the single query in which values are passed , Any help would be greatly appreciated.

Foi útil?

Solução

You can't use in if these are being passed as parameters. The easiest way is to construct a query string that creates the above query. This also has the advantage that Oracle can take advantage of indexes for the query.

You can express what you want to do using string operations as well:

select *
from Test_Table
where (:v_countries = 'ALL' or ','||:v_countries||',' like '%,'||country||',%') and
      (:v_regions = 'ALL' or ','||:v_regions||',' like '%,'||region||',%');

However, I would recommend that you use application logic to create the correct where clause rather than using this trick.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top