- How to use BIND variable for SQL statement with IN clause ?
- One of the question I faced in my last interview (about 3 months back) is "How do you use Bind Variable of SQL query with dynamic IN clause in JDBC?". It's irrelvant what I answered then, however while doing SQL Tuning task for the applicaiton I currently work I stumbled on identical queries with hard parsing. The reason for hard parsing - dynamic IN value set is concatenated to the SQL statement rather than using bind variable as there is not direct mechanism to use bind variables for dynamic IN value set. After searching through AskTom (my favorite site!!) found a way to use bind variables:
-
SQL> create type num as table of number;
SQL> create or replace function CUSTOM_IN_LIST(in_list IN VARCHAR2)
RETURN NUM
as
built_in_datatype dbms_utility.uncl_array;
array_len binary_integer :=1;
return_array num := num();
begin
dbms_utility.comma_to_table(in_list,array_len,built_in_datatype);
for indx in built_in_datatype.first .. built_in_datatype.last-1
loop
return_array.extend;
return_array(indx) := to_number(replace(built_in_datatype(indx),'"'));
end loop;
return return_array;
end;
After this all that needs to be done is replace the SQL code like below
SELECT * FROM dept WHERE deptno IN (10,20,30);
to
SELECT * FROM dept WHERE deptno IN
(select * from TABLE(CUSTOM_IN_LIST('"10","20","30"'))); - This is comparitively easy as all one have to do in concatenate the function call to SQL string.
Sunday, May 07, 2006
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment