﻿select client_out_id, * from pbx_cliente where clo_id = 1 and client_status::int <> 21 and coalesce(client_out_id, 0) > 0;

update pbx_cliente set client_out_id  = null where clo_id = 1 and client_status::int <> 21 and coalesce(client_out_id, 0) > 0;

begin

update pbx_cliente
   set client_out_id = pbx_cliente_bk_prevenda.client_out_id
  from pbx_cliente_bk_prevenda 
 where pbx_cliente.client_id = pbx_cliente_bk_prevenda.client_id
   and pbx_cliente_bk_prevenda.clo_id = 1 
   and pbx_cliente_bk_prevenda.client_status::int <> 21 
   and coalesce(pbx_cliente_bk_prevenda.client_out_id, 0) > 0;

commit

 rollback

/*
select * from pbx_cliente_bk_prevenda where clo_id = 1 and client_status::int <> 21 and coalesce(client_out_id, 0) > 0;

select * into pbx_cliente_bk_prevenda from pbx_cliente

select * from pbx_cliente_bk_prevenda
select ctst_ID, ctst_nome, count(*) 
from pbx_cliente  a, pbx_campanha_contato_status b
where b.ctst_id = a.client_status::int
group by ctst_ID, ctst_nome order by 1;

*/