我正在使用 8.2 版 Postresql
几天来一直在尝试这样做,但我一直在碰壁。如何显示以下内容:
源表 || 源列 || 目标视图 || 目标列
Stg_table1|| 客户ID || 大众客户 || Customer_details_id
我遇到的最大问题是我无法将视图中的别名链接回 Source_column。这是因为 Source_table 中列的 Ordinal 位置与 Target_view 中的列不同。
我尝试过使用这些脚本,但没有成功(这会将列重新连接到使用信息模式和序数位置)
select distinct
a.attname AS SOURCE
, a.attname::information_schema.sql_identifier AS column_name
--, format_type(a.atttypid, NULL) AS Source_Type
, d.refobjid::regclass AS Source_table
, r.ev_class::regclass AS Target_View
--, pt.Typname AS TYPE, a.*
--, c.Column_name AS Target
--, c.Data_type
from pg_attribute as a
join pg_depend as d on d.refobjid = a.attrelid and d.refobjsubid = a.attnum
join pg_rewrite as r on d.objid = r.oid
--join information_schema.columns c ON a.attnum = c.ordinal_position --and r.ev_class::regclass = c.table_schema||'.'||c.table_name
--join pg_type as pt on a.atttypid = pt.oid
JOIN pg_class
ON pg_class.oid = a.attrelid
AND a.attnum > 0
where
r.ev_class = 'abc.vw_customer'::regclass
and c.table_schema||'.'||c.table_name = 'vw_customer';
我也试过这样:
SELECT distinct dependent.relname, pg_attribute.attname
, pg_attrdef.*
--, pg_attribute.attname::information_schema.sql_identifier AS column_name
--, pg_depend.objid, pg_attribute.*
FROM pg_depend
JOIN pg_rewrite ON pg_depend.objid = pg_rewrite.oid
JOIN pg_class as dependee ON pg_rewrite.ev_class = dependee.oid
JOIN pg_class as dependent ON pg_depend.refobjid = dependent.oid
JOIN pg_attribute ON pg_depend.refobjid = pg_attribute.attrelid
--AND pg_depend.refobjsubid = pg_attribute.attnum
LEFT JOIN pg_attrdef ON pg_attribute.attrelid = pg_attrdef.adrelid AND pg_attribute.attnum = pg_attrdef.adnum
WHERE dependee.relname = 'vw_customer'
--and attname in ('Customer_details_id', 'customerid')
AND pg_attribute.attnum > 0