PG_Depend列别名(视图)到源列(表)映射依赖性

时间:2015-11-05 03:41:19

标签: postgresql metadata catalog information-schema postgresql-8.2

我正在使用版本8.2 Postresql

现在已经尝试这样做几天了,我一直在打同一面墙。如何显示以下内容:

来源表|| Source_column ||目标视图||目标栏

Stg_table1 || customerid || vw_customer || 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 

0 个答案:

没有答案