Mittwoch, 10. September 2014

Example: Kopieren einer Tabelle per PL/SQL-Block

DECLARE
  l_sql           varchar2(32767);
  c_tab_comment   varchar2(32767);
  PROCEDURE run(p_sql varchar2) AS
  BEGIN
    execute immediate p_sql;
 
  END;
BEGIN
  run('create table EMP_TMP as select * from EMP where ' || 11 ||
      ' = 1');
  select comments
    into c_tab_comment
    from sys.all_TAB_comments
   where owner = 'TEST'
     and table_name = 'EMP'
     and comments is not null;
  run('comment on table TEST.EMP is ' || '''' || c_tab_comment || '''');

  for tc in (select column_name
               from sys.all_tab_cols
              where owner = 'TEST'
                and table_name = 'EMP') loop
    for c in (select comments
                from sys.all_col_comments
               where owner = 'TEST'
                 and table_name = 'EMP'
                 and column_name = tc.column_name) loop
      run('comment on column TEST.EMP.' || tc.column_name ||
          ' is ' || '''' || c.comments || '''');
    end loop;
  end loop;
EXCEPTION
  WHEN OTHERS THEN
    NULL;
END;

########################################################

CREATE TABLE new_table
  AS (SELECT *
      FROM old_table WHERE 1=2);

aus:
[http://www.techonthenet.com/sql/tables/create_table2.php]