如何将旧列复制到新的现有表中?

时间:2017-01-25 06:15:05

标签: sql oracle

我有一个没有列的现有新表。我想复制旧表中的所有10列。我怎么做?我不想放弃表格,所以我可以执行:create table newTable as select * from oldTable

2 个答案:

答案 0 :(得分:0)

  

我不想放弃桌子,所以我可以执行:
  从oldTable创建表newTable作为select *。

假设您的新表至少有一列BobC告诉您没有任何列的表,您可以更改您的表。

Alter new_table add (col1 varchar2(10)) , col2 ...);

请注意,您需要在此处手动提及与旧表相同的所有列。

如果您不想手动执行此操作,那么可能需要PLSQL块才能执行此操作。

CREATE OR REPLACE PROCEDURE creat_tbl_frm_tbl (tablname VARCHAR2)
AS
   db_user    VARCHAR2 (100) := USER;

   TYPE t_rec IS RECORD
   (
      COLUMN_NAME   VARCHAR2 (30 BYTE),
      DATA_TYPE     VARCHAR2 (106 BYTE),
      DATA_LENGTH   NUMBER
   );

   TYPE t_record IS TABLE OF t_rec;

   -- initialization of record
   v_record   t_record := t_record ();
   v_cntr     NUMBER := 0;
   v_status   NUMBER;
   sql_stmt   VARCHAR2 (2000);
BEGIN
   ---dropping Already Existing temporary table

   BEGIN
      SELECT 1
        INTO v_status
        FROM all_objects
       WHERE UPPER (object_name) = UPPER (tablname) AND owner = db_user;

      --dbms_output.put_line( v_status );

      IF v_status = 1
      THEN
         sql_stmt := 'drop table  ' || tablname || '_new';

         EXECUTE IMMEDIATE sql_stmt;
      END IF;
   EXCEPTION
      WHEN NO_DATA_FOUND
      THEN
         DBMS_OUTPUT.put_line ('Table not found--' || tablname);
      WHEN OTHERS
      THEN
         DBMS_OUTPUT.put_line (
            'Error while dropping table-' || SQLCODE || SQLERRM);
   END;


   ---------------------------------------------------------------------------
   ---- retrieving the columns of the table
   BEGIN
        SELECT column_name, data_type, data_length
          BULK COLLECT INTO v_record
          FROM all_tab_columns
         WHERE table_name = UPPER (tablname)
      ORDER BY column_id;
   EXCEPTION
      WHEN OTHERS
      THEN
         DBMS_OUTPUT.put_line (
            'Error while retrieving the column details-' || SQLCODE || SQLERRM);
   END;


   FOR i IN 1 .. v_record.COUNT
   LOOP
      v_cntr := v_cntr + 1;

      IF v_cntr = 1
      THEN
         IF    v_record (i).data_type = 'DATE'
            OR UPPER (v_record (i).data_type) = 'TIMESTAMP(6)'
         THEN
            sql_stmt :=
                  'create table '
               || ' '
               || tablname
               || '_new'
               || '('
               || v_record (i).column_name
               || ' '
               || v_record (i).data_type
               || ')';
         ELSE
            sql_stmt :=
                  'create table '
               || ' '
               || tablname
               || '_new'
               || '('
               || v_record (i).column_name
               || ' '
               || v_record (i).data_type
               || '('
               || v_record (i).data_length
               || '))';
         END IF;

         --dbms_output.put_line(sql_stmt);
         EXECUTE IMMEDIATE sql_stmt;
      ELSE
         IF    v_record (i).data_type = 'DATE'
            OR UPPER (v_record (i).data_type) = 'TIMESTAMP(6)'
         THEN
            sql_stmt :=
                  'alter table'
               || ' '
               || tablname
               || '_new'
               || ' '
               || 'add ('
               || v_record (i).column_name
               || ' '
               || v_record (i).data_type
               || ')';
         ELSE
            sql_stmt :=
                  'alter table'
               || ' '
               || tablname
               || '_new'
               || ' '
               || 'add ('
               || v_record (i).column_name
               || ' '
               || v_record (i).data_type
               || '('
               || v_record (i).data_length
               || '))';
         END IF;

         --dbms_output.put_line(sql_stmt);
         EXECUTE IMMEDIATE sql_stmt;
      END IF;
   END LOOP;

   DBMS_OUTPUT.put_line ('Process Completed Successfully..!!');
END;                    

输出:

SQL>  exec creat_tbl_frm_tbl('table2')
Process Completed Successfully..!!

PL/SQL procedure successfully completed.

SQL> select * from table2;

no rows selected

-- This is created with same columns as of passed table table2
SQL> select * from table2_new;     
no rows selected

答案 1 :(得分:-3)

SELECT col1,col2,col3 --...
INTO newTable
FROM oldTable