-- 测试数据插入范本-- insert into tzq_check_table_structure_t(source_system, table_name, table_comment, column_name, column_type, column_comment)
三、查询表结构结果
-- 查询表结构结果WITH schemainfo AS(SELECT pg_namespace.oid,
pg_namespace.nspname
FROM pg_namespace
), tbinfo AS(SELECT pg_class.oid,
pg_class.relname,(col_description(pg_class.oid,0))::charactervaryingAScomment,
pg_class.relkind,
pg_class.relnamespace
FROM pg_class
), colinfo AS(SELECT pg_attribute.attrelid,
pg_attribute.attname,
pg_attribute.attnum,(format_type(pg_attribute.atttypid, pg_attribute.atttypmod))::charactervaryingAS typelen,(col_description(pg_attribute.attrelid,(pg_attribute.attnum)::integer))::charactervaryingAScommentFROM pg_attribute
),tab_info_v as(SELECT schemainfo.nspname ASschema,
tbinfo.relname AS table_name,
tbinfo.commentAS table_comment,
colinfo.attnum AS column_number,
colinfo.attname AS column_name,CASEWHEN((colinfo.typelen)::text='bigint'::text)THEN'INT8'::charactervaryingWHEN((colinfo.typelen)::text='smallint'::text)THEN'INT2'::charactervaryingWHEN((colinfo.typelen)::text='integer'::text)THEN'INT4'::charactervaryingWHEN((colinfo.typelen)::text='text'::text)THEN'TEXT'::charactervaryingWHEN((colinfo.typelen)::text='oid'::text)THEN'OID'::charactervaryingWHEN("left"((colinfo.typelen)::text,17)='character varying'::text)THEN(replace((colinfo.typelen)::text,'character varying'::text,'VARCHAR'::text))::charactervaryingWHEN("left"((colinfo.typelen)::text,9)='character'::text)THEN(replace((colinfo.typelen)::text,'character'::text,'CHAR'::text))::charactervaryingWHEN((colinfo.typelen)::text='timestamp without time zone'::text)THEN'TIMESTAMP'::charactervaryingWHEN((colinfo.typelen)::text='timestamp(6) without time zone'::text)THEN'TIMESTAMP'::charactervaryingWHEN((colinfo.typelen)::text='numeric(38,10)'::text)THEN'NUMERIC(38,10)'::charactervaryingELSE colinfo.typelen
ENDAS column_type,
colinfo.commentAS column_comment
FROM tbinfo,
colinfo,
schemainfo
WHERE((tbinfo.oid = colinfo.attrelid)AND(schemainfo.oid = tbinfo.relnamespace)AND(colinfo.attnum >0)AND(tbinfo.relkind =ANY(ARRAY['r'::"char",'p'::"char"]))AND(tbinfo.relname !~~'pg_%'::text)AND(tbinfo.relname !~~'sql_%'::text)AND(colinfo.attname !~~'%.....pg.dropped%'::text))ORDERBY schemainfo.nspname, tbinfo.relname, colinfo.attnum)select table_name
,table_comment
,column_name
,column_type
,column_comment
from tab_info_v
where1=1and(table_name like'%\_t'OR table_name like'%\_ti'OR table_name like'%\_tmp')and table_name notlike'act\_%'-- 查结尾不是数字的表名andnot table_name ~'[0-9]{1}$'and table_name notlike'%\_bak'orderby table_name,column_name
;
四、PostgreSQL查表名注释表字段名类型注释
-- insert into tzq_check_table_structure_t(source_system, table_name, table_comment, column_name, column_type, column_comment) WITH schemainfo AS(SELECT pg_namespace.oid,
pg_namespace.nspname
FROM pg_namespace
), tbinfo AS(SELECT pg_class.oid,
pg_class.relname,(col_description(pg_class.oid,0))::charactervaryingAScomment,
pg_class.relkind,
pg_class.relnamespace
FROM pg_class
), colinfo AS(SELECT pg_attribute.attrelid,
pg_attribute.attname,
pg_attribute.attnum,(format_type(pg_attribute.atttypid, pg_attribute.atttypmod))::charactervaryingAS typelen,(col_description(pg_attribute.attrelid,(pg_attribute.attnum)::integer))::charactervaryingAScommentFROM pg_attribute
),tab_info_v as(SELECT schemainfo.nspname ASschema,
tbinfo.relname AS table_name,
tbinfo.commentAS table_comment,
colinfo.attnum AS column_number,
colinfo.attname AS column_name,CASEWHEN((colinfo.typelen)::text='bigint'::text)THEN'INT8'::charactervaryingWHEN((colinfo.typelen)::text='smallint'::text)THEN'INT2'::charactervaryingWHEN((colinfo.typelen)::text='integer'::text)THEN'INT4'::charactervaryingWHEN((colinfo.typelen)::text='text'::text)THEN'TEXT'::charactervaryingWHEN((colinfo.typelen)::text='oid'::text)THEN'OID'::charactervaryingWHEN("left"((colinfo.typelen)::text,17)='character varying'::text)THEN(replace((colinfo.typelen)::text,'character varying'::text,'VARCHAR'::text))::charactervaryingWHEN("left"((colinfo.typelen)::text,9)='character'::text)THEN(replace((colinfo.typelen)::text,'character'::text,'CHAR'::text))::charactervaryingWHEN((colinfo.typelen)::text='timestamp without time zone'::text)THEN'TIMESTAMP'::charactervaryingWHEN((colinfo.typelen)::text='timestamp(6) without time zone'::text)THEN'TIMESTAMP'::charactervaryingWHEN((colinfo.typelen)::text='numeric(38,10)'::text)THEN'NUMERIC(38,10)'::charactervaryingELSE colinfo.typelen
ENDAS column_type,
colinfo.commentAS column_comment
FROM tbinfo,
colinfo,
schemainfo
WHERE((tbinfo.oid = colinfo.attrelid)AND(schemainfo.oid = tbinfo.relnamespace)AND(colinfo.attnum >0)AND(tbinfo.relkind =ANY(ARRAY['r'::"char",'p'::"char"]))AND(tbinfo.relname !~~'pg_%'::text)AND(tbinfo.relname !~~'sql_%'::text)AND(colinfo.attname !~~'%.....pg.dropped%'::text))ORDERBY schemainfo.nspname, tbinfo.relname, colinfo.attnum)-- (source_system, table_name, table_comment, column_name, column_type, column_comment),t as(select'prac2'as source_system
,table_name
,table_comment
,column_name
,column_type
,column_comment
from tab_info_v
where1=1and(table_name like'%\_t'OR table_name like'%\_ti'OR table_name like'%\_tmp')and table_name notlike'act\_%'-- 查结尾不是数字的表名andnot table_name ~'[0-9]{1}$'and table_name notlike'%\_bak'orderby table_name,column_name
)
五、两个环境表结构数据比对
-- 两个环境表结构数据比对-- with t as (-- select table_name, table_comment, column_name, column_type, column_comment-- from tzq_check_table_structure_t-- where source_system = 'prac'-- ),p as(select table_name, table_comment, column_name, column_type, column_comment
from tzq_check_table_structure_t
where source_system ='生产')select'prac'as source
-- ,t.* ,table_name
,table_comment
,column_name
,column_type
,column_comment
from t
EXCEPTselect'prac'as source,p.*from p
unionALLselect'生产'as source,p.*from p
EXCEPTselect'生产'as source
-- ,t.* ,table_name
,table_comment
,column_name
,column_type
,column_comment
from t
orderby table_name,column_name,1;