Using INSERT ... SELECT to Populate New Table - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
ft:locale
en-US
ft:lastEdition
2024-12-11
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

Use the INSERT ... SELECT statement to load the current data rows quickly into a new table.

The procedure for recreating a table is as follows:

  1. Select the explicit privileges of the old table with the following query:
    SELECT username, accessright, grantauthority, columnname, allnessflag FROM dbc.allrightsV  
    WHERE tablename = 'Employee' AND databasename = 'Personnel';
    Save the output for later use. You must recreate the explicit privileges on the new table.
  2. Create a new table with a temporary name, such as Temp.
    CREATE TABLE Temp_Employee
    (col1 datatype, col2 datatype...)

    To display the DDL for the current table, submit a SHOW TABLE or SHOW IN XML TABLE request.

  3. If any data types, columns, column attributes, or the column order are not compatible, use an INSERT ... SELECT statement that constructs compatible values. If this is not possible, you may need to load data from an external source.

    If the data types are compatible, you can transfer all data rows from the old table to the new table with a single INSERT ... SELECT statement:

    INSERT INTO Temp_Employee
    SELECT *
    FROM Employee ;

    If the tables are not compatible, a more complicated SELECT that computes the values for the target table from columns of the source table is needed.

  4. Use SHOW JOIN INDEX or SHOW IN XML JOIN INDEX to see the index definitions. If needed, drop the join index using DROP JOIN INDEX and recreate it using CREATE JOIN INDEX.
  5. Drop the old Employee table:
    DROP TABLE Employee ;
    When the table is dropped, explicit privileges are also dropped because the Data Dictionary references objects by ID rather than by name.
  6. Rename the temporary table:
    RENAME TABLE Temp_Employee TO Employee ;
  7. Use the index definitions from step 4 to recreate join indexes that you want to maintain for the new table.
  8. Submit GRANT statements to re-establish the explicit privileges you saved in step 1 on the new version of Employee table.

Use the LOGGING ERRORS option to log errors. Errors include duplicate row errors, duplicate primary key errors, CHECK constraint errors, and more. For more information, see INSERT/INSERT ... SELECT .