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*/

猜你喜欢

转载自blog.csdn.net/u013452335/article/details/84402478
今日推荐