UPDATE contaapagar_r set codigo=CR.codigo
FROM
(SELECT c.codigo, c1.oid
	FROM docdeorigem_r d,
	contaareceber_r c, docdeorigem_r d1, contaapagar_r c1
	where d.oid = c.rdocdeorigem
	and d1.oid=c1.rdocdeorigem and d.codigo=d1.codigo and c.vencimento=c1.vencimento
	and c.valornamoeda1=c1.valornamoeda1
	and c.emissao BETWEEN '20081009' and '20081009'
) CR
WHERE contaapagar_r.oid=CR.oid