I have a hefty SQL statement with unions where code keeps getting re-used. I was hoping to find out if there is a way to re-use a single bind variable without repeating the variable to for “USING” multiple times.
The code below returns “not all variables bound” until I change the “USING” line to “USING VAR1,VAR2,VAR1;”
I was hoping to avoid that as I’m referring to :1 in both instances – any ideas?
declare
var1 number :=1;
var2 number :=2;
begin
execute immediate '
select * from user_objects
where
rownum = :1
OR rownum = :2
OR rownum = :1 '
using var1,var2;
end;
/
EDIT: For additional info, I am using dynamic SQL as I also generate a bundle of where conditions.
I’m not great with SQL arrays (I am using a cursor in my code but I think that will overcomplicate the issue) but the pseudocode is:
v_where varchar2(100) :='';
FOR i in ('CAT','HAT','MAT') LOOP
v_where := v_where || ' OR OBJECT_NAME LIKE ''%' || i.string ||'%''
END;
v_where := ltrim(v_where, ' OR');
And then modifying the SQL above to something like :
execute immediate '
select * from user_objects
where
rownum = :1
OR rownum = :2
OR rownum = :1 AND ('||V_WHERE||')'
using var1,var2;
There are some options you might consider, although they may require changes, either to how you execute your SQL statement or to your SQL statement itself.
DBMS_SQLinstead ofEXECUTE IMMEDIATE—DBMS_SQL(see http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sql.htm) is harder to use thanEXECUTE IMMEDIATE, but gives you more control over the process — including the ability (throughDBMS_SQL.BIND_VARIABLEandDBMS_SQL.BIND_ARRAY) to bind by name instead of by position.EXECUTE IMMEDIATEwith aWITHclause — You might be able restructure your query to useWITHclause that gathers your bind variables in subquery at the beginning, and then joins to the subquery (instead of referencing the bind variables directly) whenever it needs them. It might look something like thisThis could affect the performance of your query, but it might be an acceptable compromise.
EDIT: If you’re using dynamic SQL because you have a variable number of
ORconditions like you posted in your edit, you might be able to avoid using dynamic SQL by doing one of the following:ORcriteria come from a table (or query) — Join to that table (or query) instead of using a list ofORcriteria. For example, if CAT, HAT, and MAT are listed in a column namedYOUR_CRITERIAin a table namedYOUR_CRITERIA_TABLEyou might addYOUR_CRITERIA_TABLEto theFROMclause and replace theOBJECT_NAME LIKE '%CAT% OR OBJECT_NAME LIKE '%MAT% OR OBJECT_NAME LIKE '%HAT% OR OBJECT_NAME LIKE '%MAT%in theWHEREclause with something likeOBJECT_NAME LIKE '%' || YOUR_CRITERIA_TABLE.YOUR_CRITERIA || '%'.CREATE TYPE...IS TABLE OF) instead of a global temporary table. You could create or own nested table type, or use a built-in one likeSYS.ODCIVARCHAR2LIST. In PL/SQL, you would populate an variable of this type, and then use it like a “real” table like in item 1.An example of item 3 might look something like: