1. 程式人生 > >PGSQL資料庫升級指令碼對比sql

PGSQL資料庫升級指令碼對比sql

with nms130tables as (select * from dblink('host=127.0.0.1 dbname=nms_130 user=postgres password=Hik12345+','select schemaname, tablename from pg_tables') as t(schemaname varchar,tablename varchar)),

nms130columns as (select * from dblink('host=127.0.0.1 dbname=nms_130 user=postgres password=Hik12345+','select table_catalog, table_schema, table_name, column_name, data_type, ordinal_position from information_schema.columns
') as t(table_catalog varchar,table_schema varchar,table_name varchar, column_name varchar, data_type varchar, ordinal_position int)
where table_catalog = 'nms_130' and table_schema = 'public'

 --修改,110有,130也有,表名一致、欄位名稱一致、欄位型別不一致(欄位長度暫時不考慮)
 select cl.table_catalog,
                 cl.table_name,
                 cl.column_name,
                 cl.data_type
 from nms130columns cl
 where 1 = 1
 and EXISTS (select 1 from information_schema.columns tab where table_catalog = 'nms_120' and table_schema = 'public' and tab.table_name = cl.table_name and tab.column_name = cl.column_name and tab.data_type <> cl.data_type)
 ORDER BY cl.table_name, cl.ordinal_position

-- 新增、110沒有,130有的表名一致、欄位名一致、段型別一致
 /*select cl.table_catalog,
                 cl.table_name,                 
cl.column_name,
                 cl.data_type
 from nms130columns cl
where 1 = 1
 and not EXISTS (select 1 from information_schema.columns tab where table_catalog = 'nms_120' and table_schema = 'public' and tab.table_name = cl.table_name and tab.column_name = cl.column_name)
ORDER BY cl.table_name, cl.ordinal_position*/

-- 刪除、110有、120沒有的表名一致、欄位名一致的型別
/*select cl.table_catalog,
                cl.table_name,
                cl.column_name,
                cl.data_type
from information_schema.columns cl
where cl.table_catalog = 'nms_130' and cl.table_schema = 'public'
and not exists (select 1 from nms130columns tab where tab.table_name = cl.table_name and tab.column_name = cl.column_name)
ORDER BY cl.table_name, cl.ordinal_position*/