Tom,
We are using 9i.
I read your articles:
</code>
http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:139812348065 about binding list, and
http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:1288401763279 <code>
about sys_context.
I implement a general-use FILTER_TY. The types created under this add the
table-dependent logic, but all types would use the same interface to
supply parameters and get the result set. I would like to use bind
variables and return a ref cursor to the clinet (which is c# using odp.net).
The parameters passed to the FILTER_TY are of two types.
Either a scalar value or a list. The number of parameters of each type are
unknown, which is the problem when using 'execute immediate' or 'open ... for'.
Based on the above mentioned articles I can think of two possible solutions:
1. For binding scalar values I can use sys_context with the appropriate
type conversion.
For binding lists I can use nested tables If I limit the number of
parameters of this type.
It will look something like this:
create type VARCHAR2_TAB_TY is table of VARCHAR2(255);
create type VARCHAR2_TAB_TAB_TY is table of VARCHAR2_TAB_TY;
procedure proc1 (c out sys_refcursor)
scalar_value_1 VARCHAR2(255);
scalar_value_2 VARCHAR2(255);
list_values VARCHAR2_TAB_TAB_TY; -- in real, these parameters are
-- passed to the FILTER_TY
v_sql VARCHAR2(32767) := 'select a,b,c from tab where ';
c is ref cursor;
begin
-- for each scalar value I can:
v_sql := v_sql || 'col_1 = TO_NUMBER(sys_context( ''MY_CTX'',
''scalar_value_1'' )) ';
dbms_session.set_context( 'MY_CTX', 'scalar_value_1', scalar_value_1);
-- for each list value that is set or NOT set (empty) I can:
v_sql := v_sql || 'col_2 in (select * from TABLE(:X)) ';
...
-- and then opening the cursor like:
open c for v_sql
using list_values(1), list_values(2), ..., list_values(n);
-- n is a constant, the maximum number of list_values the rutine can handle.
-- 30 should be enough.
end;
2. For binding scalar values I can use sys_context with the appropriate type
conversion.
For binding lists I can use a plsql table. In this case I have to convert
lists stored in nested tables into comma separated simple varchar2
list, and then I can bind it like scalar values thru sys_context.
This approach is not usable though, because sys_context values can
store values only up to 255 chars.
Q: Is there any better way?
In case 1, will binding of empty nested tables cause any problem?
Thank you very much Tom. Looking forward to your answer.