I'm receiving different versions of an xml file each defined in different namespace. So i need to use the namespace as bind variable in my dynamic query.
On Oracle 12.1, the statement runs well with hardcoded namespace: XMLNAMESPACES (DEFAULT 'http://www.ff.org')
.
If i try to replace the string with a bind variable, it throws an error due to missing single quotes. I've already tried to add the single quotes when binding the variable in "execute immediate" or adding quotes in dynamic query enclosing the bind variable :sNamespace, non of them worked.
declare
sNamespace varchar2(100);
sXMLVersion VARCHAR2(3);
sstmt varchar2(1000);
begin
sNamespace := 'http://www.ff.org';
sstmt := q'#
with t_base as (select xmltype('<froot xmlns="http://www.ff.org">
</froot>') as xml from dual)
SELECT case when v.is_root = 1 then '1'
else '2' end
into :sXMLVersion
FROM t_base t,
XMLTABLE(XMLNAMESPACES(DEFAULT :sNamespace),
'$d' passing t.xml as "d"
columns
is_root INTEGER PATH 'exists(froot)'
) v
#';
execute immediate sstmt using sNamespace into sXMLVersion;
dbms_output.put_line(sXMLVersion);
end;