Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

querying all_tab_columns

523861Jul 25 2011 — edited Jul 25 2011
Hi all,

This is a "my query takes too long" question but it relates to the ALL_TAB_COLUMNS view.

I'm trying to generate some basic DML scripts from a large number of tables (insert/update) the cheating way by selecting columns from all_tab_columns

e.g.
select 'insert into '||table_name||' ('
  from all_tables
 where table_name = 'TABLE_NAME_HERE'
union all
select column_name||','
  from all_tab_columns
where table_name = 'TABLE_NAME_HERE'
--etc.
however, I want the column names all on one line.

so I have the following, which I imagine should create the column_name's on one line but it "runs for ages"
select sys_connect_by_path(column_name,', ') c
  from all_tab_columns
 where owner = 'WHITEHAT'
   and table_name = 'FOO'
   and connect_by_isleaf = 1
   start with column_id = 1
   connect by column_id = prior column_id + 1
I am using 10gR2 so unfortunately I can't use recursive subquery factoring just yet, although that equivalent runs quickly as expected on our 11g environment:
with t (column_name, column_id) 
             as (select cast(column_name as varchar2(4000)), column_id
                   from all_tab_columns
                  where owner = 'WHITEHAT'
                    and table_name = 'FOO'
                    and column_id = 1
                  UNION ALL
                  SELECT cast(t.column_name||', '||atc.column_name as varchar2(4000))
                        ,atc.column_id
                    from t
                        ,all_tab_columns atc
                   where owner = 'WHITEHAT'
                    and table_name = 'FOO'
                    and atc.column_id = t.column_id + 1
                 )
select column_name
  from (
         select column_name, column_id, max(column_id) over () max_col_id
         from t           
       )
 where column_id = max_col_id
what am I doing wrong?
This post has been answered by Frank Kulash on Jul 25 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 22 2011
Added on Jul 25 2011
2 comments
1,652 views