update supermag.smwaybillsin wi set(supplflags, debt)= (select 1, d.totalsum from supermag.smdocuments d where d.doctype='WI' and d.id=wi.id) where supplflags=0 and id in (select id from supermag.smdocuments where doctype='WI' and opcode=0 and locationto=:id and createdat between :dat1 and :dat2)