I have query building plsql code. I use bind variables and associative array to store their values. Something like that:
declare
type myt table of varchar2(4000) index by varchar2(100);
vars myt;
v_cursor integer;
newQuery varchar2(1000) := 'select * from blabla where ';
bind_key varchar2(100);
rows_count integer;
begin
-- query building
if 1=1 then
newQuery := newQuery || 'col1 = :bind_col1';
vars(':bind_col1') := sysdate; -- problem!!!
end if;
....
-- query execution
v_cursor := dbms_sql.open_cursor;
dbms_sql.parse(v_cursor, newQuery, dbms_sql.v7);
bind_key := vars.first;
loop
exit when bind_key is null;
dbms_sql.bind_variable(v_cursor, bind_key, vars(bind_key));
bind_key := vars.next(bind_key);
end loop;
row_count := dbms_sql.execute(v_cursor);
dbms_sql.close_cursor(v_cursor);
end;
Are there any more generic type than varchar2, so there will not be conversion date->varchar2->date?
dbms_sql.bind_variablecan bind variables of many datatypes. You can’t however declare a table of<any_data_type>(which would be logically equivalent to something likeObject[]in java for example.)When you’re working with VARCHAR2 you can use bijective explicit conversion, for example: