changing coulmn name in where caluse depending on value passed [message #377774] |
Thu, 25 December 2008 01:51 |
smithagirish
Messages: 10 Registered: March 2008 Location: kochi
|
Junior Member |
|
|
hello all
table_a
-------
po_no number
so_no number
amt number
i have a SQL
select * from table_a where so_no(+) = &v_po_so_no
now this will work fine if "&v_po_so_no" contains So_no value
but if i am passing a po_no value to "&v_po_so_no" then
the sql should be like this
select * from table_a where po_no(+) = &v_po_so_no
how can i write a single query for both
thank you
|
|
|
|
|
|
|
Re: changing coulmn name in where caluse depending on value passed [message #377845 is a reply to message #377844] |
Thu, 25 December 2008 21:57 |
|
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
yes, that will work, but you will find that performance may be very bad because you will not be able to use indexes on po_no or so_no. That is why instead you might wish to consider manually partitioned views (or manually partitioned queries as the case may be). There may also be a simple OR alternative coding that will also work, but I am not sure the or is easy to code given the outerjoins you have. Consider this:
select * from table_a where 'PO_NO' = '&po_so' and PO_NO = &v_po_so_no
union all
select * from table_a where 'SO_NO' = '&po_so' and SO_NO = &v_po_so_no
/
In the above, only one of the two queries will be executed because Oracle will remove the one that does not match your parameter and will do this at the begining of execution (or possibly bind time).
Additionally, if indexes exist on PO_NO and SO_NO, then they might actually be used if the optimizer likes what it sees for stats.
In your decode example, indexes on PO_NO and SO_NO will not be used (unless Oracle Optimizer has gotten real smart in the last release or two).
Good luck, Kevin
|
|
|