do $$ begin
	IF NOT EXISTS(select * from information_schema.columns WHERE table_name='T_ADDRESS' and column_name='aoguid')
then
	ALTER TABLE AKUZ.T_ADDRESS ALTER COLUMN "aoguid" TYPE text;
END IF;
end; $$;

do $$ begin
	IF NOT EXISTS(select * from information_schema.columns WHERE table_name='T_ADDRESS' and column_name='houseguid')
then
	ALTER TABLE AKUZ.T_ADDRESS ALTER COLUMN "houseguid" TYPE text;
END IF;
end; $$;

update addr
set HouseGuid = fulladdr.HouseGuid, 
	AOGUID = fulladdr.AOGUID
from akuz.T_ADDRESS addr
	join fias.T_FullADDR2 fulladdr on fulladdr.AddrID = addr.FullADDR2
where addr.FullADDR2 is not null;

update addr
set HouseGuid = fulladdr.HouseGuid, 
	AOGUID = fulladdr.AOGUID
from akuz.T_ADDRESS addr
	join fias.T_FullAddr fulladdr on fulladdr.AddrID = addr.Addrobj
where addr.Addrobj is not null and addr.HouseGUID is null and addr.AOGUID is null;