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!

How to create a procedure to output REF CURSOR with any WHERE clause?

user49914949919675123Oct 14 2011 — edited Oct 14 2011
I have an requirement like this: I have huge query which need to reuse in my code more than 10 times. This SQL has about 50 lines. Thing is for those 10 odd times sometimes the WHERE clause changes (columns are the same). So I cannot create a view since SQL is not static.

I thought of writing a procedure with a WHERE_CLAUSE input para. I output a sys refcursor by adding the where clause. But I can't do it since you cannot add a where clause like that.

i.e.
PROCEDURE dynamyic_query (p_where_clause IN VARCHAR2, p_out_query OUT SYS_REFCURSOR ) IS
BEGIN

  OPEN p_out_query FOR SELECT ......... FROM table WHERE || ' ' || p_where_clause;

END;
The above gives error.

How to handle a situation like this???? Any help would be greatly appreciated.
This post has been answered by Frank Kulash on Oct 14 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 11 2011
Added on Oct 14 2011
6 comments
600 views