重复刷添加表字段的sql

--ALTER TABLE nrmsdata.dim_rms_risk_query_zb_org
--DROP column risk_type5


--ALTER TABLE nrmsdata.dim_rms_risk_query_zb_org
--ADD risk_type5 varchar(10);


drop function if exists func_add_column();
create or replace function func_add_column() returns int as
$body$
begin
perform * from pg_tables where tablename = 'dim_rms_risk_query_zb_org';
if found then
perform * from pg_attribute where attrelid = 'dim_rms_risk_query_zb_org'::regclass and attname = 'risk_type5';
if not found then
alter table nrmsdata.dim_rms_risk_query_zb_org add column risk_type5 varchar(10);
end if;
end if;
return 0 ;
end;
$body$ language plpgsql;
select func_add_column();

原文地址:https://www.cnblogs.com/jcfxl/p/10998636.html