dbTalk Databases Forums  

best way to parameterize an "in (...)" part of query?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss best way to parameterize an "in (...)" part of query? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mh@pixar.com
 
Posts: n/a

Default best way to parameterize an "in (...)" part of query? - 03-01-2009 , 12:50 AM






I have two relatively large procedures that differ only
by the contents of an "in (...)" clause, e.g.

create proc1 as
...
select foo from bar where x in ('a','b')
...

create proc2 as
...
select foo from bar where x in ('x','y')
...

I would like to parameterize this, so that proc1 and proc2 can
call a common subroutine, and pass in the "in" list.

create proc1 as ... baseproc(['a','b']) -- some representation of a,b
create proc2 as ... baseproc(['x','y']) -- some representation of x,y

create baseproc(INLIST SOMETYPE)
...
select foo from bar where x in INLIST
...

How can I parameterize this? Is there an appropriate type?

Many TIA!!
Mark

--
Mark Harrison
Pixar Animation Studios

Reply With Quote
  #2  
Old   
Maxim Demenko
 
Posts: n/a

Default Re: best way to parameterize an "in (...)" part of query? - 03-01-2009 , 03:23 AM






mh (AT) pixar (DOT) com schrieb:
Quote:
I have two relatively large procedures that differ only
by the contents of an "in (...)" clause, e.g.

create proc1 as
...
select foo from bar where x in ('a','b')
...

create proc2 as
...
select foo from bar where x in ('x','y')
...

I would like to parameterize this, so that proc1 and proc2 can
call a common subroutine, and pass in the "in" list.

create proc1 as ... baseproc(['a','b']) -- some representation of a,b
create proc2 as ... baseproc(['x','y']) -- some representation of x,y

create baseproc(INLIST SOMETYPE)
...
select foo from bar where x in INLIST
...

How can I parameterize this? Is there an appropriate type?

Many TIA!!
Mark

I have serious doubts the best way exists, but there are many
alternatives which you can implement. Besides that, the question you ask
was very often discussed ( keyword: dynamic in list) here or at AskTom.
Here are some approaches:

1) select foo from bar where instr(','||INLIST||',',','||x||',') > 0
2) select foo from bar where x in ( select
regexp_substr(INLIST,'(^|,)(.*)(,|$)','\2',level,1 )
from dual
connect by regexp_substr(INLIST,'(^|,)(.*)(,|$)','\2',level,1 )
is not null)

David Fitzjarell has published on his site an approach as well
http://oratips-ddf.blogspot.com/2008...w-dynamic.html

But, as i mentioned already, this question is probably asked as often as
about pivot tables, so if you start to search, you'll find dozens of
possibilities


Best regards

Maxim


Reply With Quote
  #3  
Old   
Robert Klemme
 
Posts: n/a

Default Re: best way to parameterize an "in (...)" part of query? - 03-01-2009 , 04:42 AM



On 01.03.2009 10:23, Maxim Demenko wrote:
Quote:
mh (AT) pixar (DOT) com schrieb:
I have two relatively large procedures that differ only
by the contents of an "in (...)" clause, e.g.

create proc1 as
...
select foo from bar where x in ('a','b')
...

create proc2 as
...
select foo from bar where x in ('x','y')
...

I would like to parameterize this, so that proc1 and proc2 can
call a common subroutine, and pass in the "in" list.

create proc1 as ... baseproc(['a','b']) -- some representation
of a,b
create proc2 as ... baseproc(['x','y']) -- some representation
of x,y

create baseproc(INLIST SOMETYPE)
...
select foo from bar where x in INLIST
...

How can I parameterize this? Is there an appropriate type?

I have serious doubts the best way exists, but there are many
alternatives which you can implement. Besides that, the question you ask
was very often discussed ( keyword: dynamic in list) here or at AskTom.
Here are some approaches:

1) select foo from bar where instr(','||INLIST||',',','||x||',') > 0
2) select foo from bar where x in ( select
regexp_substr(INLIST,'(^|,)(.*)(,|$)','\2',level,1 )
from dual
connect by regexp_substr(INLIST,'(^|,)(.*)(,|$)','\2',level,1 )
is not null)
Another option: create a table type and use a query roughly like

select foo from bar where x in ( select column_value from table(array) )

Quote:
But, as i mentioned already, this question is probably asked as often as
about pivot tables, so if you start to search, you'll find dozens of
possibilities
That's true.

Kind regards

robert


Reply With Quote
  #4  
Old   
William Robertson
 
Posts: n/a

Default Re: best way to parameterize an "in (...)" part of query? - 03-02-2009 , 04:13 AM



On Mar 1, 10:42*am, Robert Klemme <shortcut... (AT) googlemail (DOT) com> wrote:
Quote:
On 01.03.2009 10:23, Maxim Demenko wrote:



m... (AT) pixar (DOT) com schrieb:
I have two relatively large procedures that differ only
by the contents of an "in (...)" clause, e.g.

* * create proc1 as
* * ...
* * * *select foo from bar where x in ('a','b')
* * ...

* * create proc2 as
* * ...
* * * *select foo from bar where x in ('x','y')
* * ...

I would like to parameterize this, so that proc1 and proc2 can
call a common subroutine, and pass in the "in" list.

* * create proc1 as ... *baseproc(['a','b']) *-- some representation
of a,b
* * create proc2 as ... *baseproc(['x','y']) *-- some representation
of x,y

* * create baseproc(INLIST SOMETYPE)
* * ...
* * * *select foo from bar where x in INLIST
* * ...

How can I parameterize this? *Is there an appropriate type?
I have serious doubts the best way exists, but there are many
alternatives which you can implement. Besides that, the question you ask
was very often discussed ( keyword: dynamic in list) here or at AskTom.
Here are some approaches:

1) select foo from bar where instr(','||INLIST||',',','||x||',') > 0
2) select foo from bar where x in ( select
regexp_substr(INLIST,'(^|,)(.*)(,|$)','\2',level,1 )
from dual
connect by regexp_substr(INLIST,'(^|,)(.*)(,|$)','\2',level,1 )
is not null)

Another option: create a table type and use a query roughly like

select foo from bar where x in ( select column_value from table(array) )

But, as i mentioned already, this question is probably asked as often as
*about pivot tables, so if you start to search, you'll find dozens of
possibilities

That's true.

Kind regards

* * * * robert
The cleanest and simplest approach might be to pass a collection into
the procedure and replace the IN clause with MEMBER OF. I'm not sure
how well that scales for large collections though.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.