

update ter_contacto set active=0 where active=1 and contacto in (
select contacto from ter_contacto where id_usuario>0 group by contacto having count(1)>1
)


declare @tmp_contacto as table (contacto varchar(100))

insert into @tmp_contacto select contacto from ter_contacto where id_usuario>0 group by contacto having count(1)>1

declare @contacto as varchar(100)
declare cur_contacto cursor for select contacto from @tmp_contacto

open cur_contacto

fetch next from cur_contacto into @contacto

while @@fetch_status = 0 
begin

update top (1) ter_contacto set active=1 where active=0 and contacto=@contacto

fetch next from cur_contacto into @contacto

end 

close cur_contacto
deallocate cur_contacto


update seg_usuario set active=0 where active=1 and usuario in (
select usuario from seg_usuario where id in(select id_usuario from ter_contacto where id_usuario>0) group by usuario having count(1)>1
)


update seg_usuario set active=1 
where active=0 and id in (
select id_usuario from ter_contacto where active=1 and id_usuario>0
) and usuario in (
select usuario from seg_usuario where id in(select id_usuario from ter_contacto where id_usuario>0) group by usuario having count(1)>1
)
