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!

Is there a way to enter an expression list in a single variable?

Jiri.Machotka-OracleAug 18 2015 — edited Aug 19 2015

I have a query like this:

SQL> select distinct t1.name from people t1 join who_likes_what t2 on t1.id = t2.people_id join hobbies t3 on t3.id = t2.hobbies_id and t3.text in ('Music', 'Horses') group by t1.name having count (t3.text) = 2;

NAME

--------------------

Bob

Amy

(2 is the number of elements in ('Music', 'Horses') )

1. Is there a way to parametrize the expression list in a single variable?

Having my brain turned off I had thought that if I created a (single) string variable containing the value '('Music', 'Horses')' I'd be done.

I understand why it cannot work, but I don't know how to replace it with a single variable.

I cannot/don't want to use (:var_1, :var_2, ..., :var_n) because the number of elements in the expression list is variable, and I should be using a SQL prepared statement.

In the other thread I have already been advised to use a single string like 'Music~Horses' and the following construct:

select regexp_substr(:set_1 || '~','[^~]+',1,level) txt from dual

but is there a way to achieve it directly?

2. Is there a function that returns the number of elements in an expression list?

(to obtain 2 from the entry for 1.)

Thank you for any suggestion.

This post has been answered by Stew Ashton on Aug 19 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 16 2015
Added on Aug 18 2015
24 comments
3,265 views