I am building an Interactive Report. Source Type is Function Body returning SQL Query. And below is my query which is prone to sql injection. How can i bind variables here?
declare
vsql varchar2(4000);
begin
vsql := 'SELECT Name FROM Customers WHERE 1 = 1 ';
--# There are other checks as well for both SELECT and WHERE which are making this query dynamic
if :P1_NAME is not null then
vsql := vsql || ' AND UPPER(NAME) LIKE Upper('%''' || :P1_NAME || '%'')';
end if;
return vsql;
end;
Here is how I would do it. The example below has dynamic table and a variable in the where clause using
LIKE.The idea is to sanitize the table name (or any other sql objects) using
DBMS_ASSERT.SQL_OBJECT_NAMEand use bind variables where possible. So the generated query will still have bind variable substitution.The double %% is needed because % is a special character in the apex_string.format function:
query in debug: