db2 - Using MERGE with XMLQUERY and transform -
using db2 luw 10.1. having trouble getting merge work on 2 tables xml columns merged using xquery transform.
i have table looks this:
create table foo ( id int not null primary key, data xml not null )
to data table, load (using load) staging table looks this:
create table foo_incoming ( id int not null, data xml not null )
the data in xml columns merged using xquery transform. there's logic behind it's not straightforward, it's not overly complicated either. have tested transform using manual updates know works.
i try merge 2 tables this:
merge foo f using (select * foo_incoming) on (f.id = i.id) when matched update set data = xmlquery(' transform copy $out := $old modify ( ... ) return $out' passing f.data "old", i.data "new") when not matched insert (id, data) values (i.id, i.data)
this works when there data in foo. xml column merged way want be. if foo empty, error:
sql16084n assigned value in copy clause of transform expression not sequence 1 item node. error qname=err:xuty0013. sqlstate=10705
it seems db2 trying evaluate xquery though merge didn't match. thus, f.data null, , copy expression in transform gets empty sequence. if remove entire "when matched" clause statement works.
what doing wrong? or limitation of db2's merge statement?
i have tried work around changing simple "when matched" "when matched , (f.data not null)" has no effect. tried changing xquery expression this:
if($old) transform copy $out := $old ... else ()
that didn't either. workaround have found split merge in twain. first this, update rows in both foo , foo_incoming:
merge foo f using ( select q1.id, q1.data foo_incoming q1 inner join foo q2 on (q1.id = q2.id) ) on (f.id = i.id) when matched udpate ...;
then this, insert rows in foo_incoming not foo:
merge foo f using ( select q1.id, q1.data foo_incoming q1 left outer join foo q2 on (q1.id = q2.id) q2.id null ) on (f.id = i.id) when not matched insert ...;
this works. expect performance abysmal, however. , sort of hack interpret warning sign there's fundamental error in thinking.
this might bug in db2 should fixed. same problem has been described , discussed here:
i hope upcoming db2 fixpack resolve problem.