Oracle
 sql >> Database >  >> RDS >> Oracle

Cerca dinamicamente i nomi delle colonne per una tabella durante una query sql

Questo PL/SQL dovrebbe farlo:

declare
    l_cols long;
    l_sql  long;
begin
    for r in (select column_name from all_tab_columns
              where  table_name = 'TABLEA'
              and    owner = 'SCHEMA1'
             )
    loop
       l_cols := l_cols || ',' || r.column_name;
    end loop;

    -- Remove leading comma
    l_cols := substr(l_cols, 2);

    l_sql := 'insert into schema1.tableA (' || l_cols || ') select ' 
             || l_cols || ' from schema2.tableA';

    execute immediate l_sql;

end;
/