I am writing a .NET utility that automatically backs up data from a source table to a history table on an hourly basis. If the archive table doesn’t exist, it has to be programatically created. Usually, the SQL of create db.historytable as db.sourcetable with no data does the trick. However, create table as does not work on tables that use identity columns. This means that you have to get the source table’s DDL, change the table’s name, and then change the identity column into an integer (because we don’t want the history table to use an identity column) before running the new DDL.
If I could get a column-level DDL for the definitions of each column in a table then I could iterate through that using a DataReader and rebuild the DDL. If the COLUMNTYPE is 2 (2 is identity column) then I will know to modify that column’s definition from:
ID INTEGER GENERATED BY DEFAULT AS IDENTITY
(START WITH 1
INCREMENT BY 1
MINVALUE 0
MAXVALUE 100000000
NO CYCLE)
..to just:
ID INTEGER
The other options which I am only interested in tryout out as a last resort are 1) using a complex regex to replace the identity column definition with a regular INTEGER or 2) query dbc.columns and dbc.tables and use the metadata there to build the entire table’s DDL from scratch.
Essentially, I am hoping that I there is something conceptually like dbc.columnddl that I can join to dbc.columns, but I am taking a long shot at that, I know.
AFAIK, there’s no ‘column DDL’ in the format you’re looking for in Teradata data dictionary. You’ll have to reconstruct types etc from DBC.Columns, as you suggested.
Also, you might find this useful: here’s how to get a list of all tables with identity columns in the system: