You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

17 lines
1.0 KiB

select 'DO $$ BEGIN BEGIN ALTER TABLE '||relname||' ADD COLUMN recordyear int4; EXCEPTION WHEN duplicate_column THEN RAISE NOTICE ''column recordyear already exists''; END; END;$$;' as tabname from pg_class c
where relkind = 'r' and relname like 'j%_r' and relname not like 'pg_%' and relname not like 'sql_%' order by relname;
select 'update ' || relname || ' set recordyear=2019;' as sqlstr from pg_class c
where relkind = 'r' and relname like 'j%_r' and relname not like 'pg_%' and relname not like 'sql_%' order by relname;
-- 设置默认值
select 'DO $$ BEGIN BEGIN ALTER TABLE '||relname||' ALTER COLUMN recordyear SET DEFAULT 2020; EXCEPTION WHEN duplicate_column THEN RAISE NOTICE ''column recordyear already exists''; END; END;$$;' as tabname from pg_class c
where relkind = 'r' and relname like 'j%_r' and relname not like 'pg_%' and relname not like 'sql_%' order by relname;
-- 删除默认值
ALTER TABLE table_name ALTER COLUMN column_name DROP DEFAULT;