数据库升级,给某张表增加字段,防止重复升级时sql脚本报错

维护某个项目时,需要对数据库中的某张表增加一个字段,未防止多次执行脚本时报错,可以使用下发方法

 1 drop function if exists func_add_column();
 2 create or replace function func_add_column() returns int as
 3 $body$
 4 begin
 5     perform * from pg_tables where tablename = '表名';
 6     if found then
 7         perform * from pg_attribute where attrelid = '表名'::regclass and attname = '字段名';
 8         if not found then
 9             alter table 表名 add column 字段名 ;
10         end if;
11     end if;
12     return 0 ;
13     end;
14 $body$ language plpgsql;
15 select func_add_column();

 

posted @ 2018-05-23 20:59  青苹果a  阅读(345)  评论(0编辑  收藏  举报