在 Oracle 中获取 SYNONYM 中的特定列名称
Get Specific Column Names in SYNONYM in Oracle
通过此查询,我可以成功获取 Oracle table.
的列名列表
SELECT COLUMN_NAME
FROM USER_TAB_COLUMNS
WHERE table_name = 'TBL_NEWS'
AND COLUMN_NAME LIKE ('GLOBE_%')
ORDER BY COLUMN_ID
我有一个同义词,我还需要获取该同义词的所有列名。那么我如何扩展上面的查询以获取同义词中以 GLOBE_
开头的所有列名?
同义词没有列,它所指的 table 有列。所以你可以使用这个:
SELECT COLUMN_NAME
FROM ALL_TAB_COLUMNS atc
JOIN ALL_SYNONYMS als ON atc.table_name = als.table_name
WHERE als.SYNONYM_NAME='my_synonym'
AND als.OWNER IN (USER, 'PUBLIC')
AND atc.COLUMN_NAME LIKE ('GLOBE_%')
ORDER BY atc.COLUMN_ID
取决于您希望从中获取数据的架构范围 - 您可以使用 ALL_TAB_COLUMNS 和 ALL_SYNONYMS 或 DBA_TAB_COLUMNS 和 DBA_SYNONYMS
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS ATC JOIN ALL_SYNONYMS ALS ON ATC.table_name = ALS.table_name WHERE ALS.SYNONYM_NAME='my_synonym' AND ATC.COLUMN_NAME LIKE ('GLOBE_%') ORDER BY ATC.COLUMN_ID;
or
SELECT COLUMN_NAME FROM DBA_TAB_COLUMNS DTC JOIN DBA_SYNONYMS DS ON DTC.table_name = DS.table_name WHERE DS.SYNONYM_NAME='my_synonym' AND DTC.COLUMN_NAME LIKE ('GLOBE_%') ORDER BY DTC.COLUMN_ID;
而不是查询数据字典。您可以在 table/view/synonym.
中的 return 列列表中创建函数
create type list_varchar2 is table of varchar2(100);
/
create or replace function list_of_column(p_name in varchar2) return list_varchar2 PIPELINED
is
v_Cnt number := 0;
v_table_description dbms_sql.desc_tab;
c_curosor integer default dbms_sql.open_cursor;
begin
dbms_sql.parse( c_curosor, 'select * from '|| p_name||' where 1 = 2', dbms_sql.native );
dbms_sql.describe_columns( c => c_curosor,
col_cnt => v_Cnt,
desc_t => v_table_description );
for i in 1 .. v_Cnt
loop
pipe row ( v_table_description(i).col_name );
end loop;
dbms_sql.close_cursor(c_curosor);
exception
when others then dbms_sql.close_cursor( c_curosor );
raise;
end ;
/
select * from table(list_of_column('table_name'))
/
select * from table(list_of_column('view_name'))
/
select * from table(list_of_column('synonym'))
/
通过此查询,我可以成功获取 Oracle table.
的列名列表SELECT COLUMN_NAME
FROM USER_TAB_COLUMNS
WHERE table_name = 'TBL_NEWS'
AND COLUMN_NAME LIKE ('GLOBE_%')
ORDER BY COLUMN_ID
我有一个同义词,我还需要获取该同义词的所有列名。那么我如何扩展上面的查询以获取同义词中以 GLOBE_
开头的所有列名?
同义词没有列,它所指的 table 有列。所以你可以使用这个:
SELECT COLUMN_NAME
FROM ALL_TAB_COLUMNS atc
JOIN ALL_SYNONYMS als ON atc.table_name = als.table_name
WHERE als.SYNONYM_NAME='my_synonym'
AND als.OWNER IN (USER, 'PUBLIC')
AND atc.COLUMN_NAME LIKE ('GLOBE_%')
ORDER BY atc.COLUMN_ID
取决于您希望从中获取数据的架构范围 - 您可以使用 ALL_TAB_COLUMNS 和 ALL_SYNONYMS 或 DBA_TAB_COLUMNS 和 DBA_SYNONYMS
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS ATC JOIN ALL_SYNONYMS ALS ON ATC.table_name = ALS.table_name WHERE ALS.SYNONYM_NAME='my_synonym' AND ATC.COLUMN_NAME LIKE ('GLOBE_%') ORDER BY ATC.COLUMN_ID;
or
SELECT COLUMN_NAME FROM DBA_TAB_COLUMNS DTC JOIN DBA_SYNONYMS DS ON DTC.table_name = DS.table_name WHERE DS.SYNONYM_NAME='my_synonym' AND DTC.COLUMN_NAME LIKE ('GLOBE_%') ORDER BY DTC.COLUMN_ID;
而不是查询数据字典。您可以在 table/view/synonym.
中的 return 列列表中创建函数create type list_varchar2 is table of varchar2(100);
/
create or replace function list_of_column(p_name in varchar2) return list_varchar2 PIPELINED
is
v_Cnt number := 0;
v_table_description dbms_sql.desc_tab;
c_curosor integer default dbms_sql.open_cursor;
begin
dbms_sql.parse( c_curosor, 'select * from '|| p_name||' where 1 = 2', dbms_sql.native );
dbms_sql.describe_columns( c => c_curosor,
col_cnt => v_Cnt,
desc_t => v_table_description );
for i in 1 .. v_Cnt
loop
pipe row ( v_table_description(i).col_name );
end loop;
dbms_sql.close_cursor(c_curosor);
exception
when others then dbms_sql.close_cursor( c_curosor );
raise;
end ;
/
select * from table(list_of_column('table_name'))
/
select * from table(list_of_column('view_name'))
/
select * from table(list_of_column('synonym'))
/