When working on a project, I had around 100 tables and was required to generate Datastage schema files for the DataStage stages, so I have come up with this SQL in Teradata to generate the schema:
SELECT
DS
FROM
(
SELECT
columnid
,
CASE WHEN f_row=1 THEN 'record' || '0D0A'XC || '{final_delim=end, delim=",", null_field='''', quote=double} ' || '0D0A'XC || '( ' ||
TRIM(columnname) || ': ' ||
CASE WHEN Nullable='Y' THEN 'nullable '
ELSE ''
END
||
CASE
WHEN ColumnType='CV' THEN ' string[max=' || TRIM(ColumnLength) || '];'
WHEN ColumnType='CF' THEN ' string[' || TRIM(ColumnLength) || '];'
WHEN ColumnType='D' THEN ' decimal[' || TRIM(DecimalTotalDigits) || ', ' || TRIM(DecimalFractionalDigits) || '];'
WHEN ColumnType='TS' THEN ' TIMESTAMP;'
END
WHEN f_row<>1
AND l_row<>1 THEN
TRIM(columnname) || ': ' ||
CASE WHEN Nullable='Y' THEN 'nullable '
ELSE ''
END
||
CASE
WHEN ColumnType='CV' THEN ' string[max=' || TRIM(ColumnLength) || '];'
WHEN ColumnType='CF' THEN ' string[' || TRIM(ColumnLength) || '];'
WHEN ColumnType='D' THEN ' decimal[' || TRIM(DecimalTotalDigits) || ', ' || TRIM(DecimalFractionalDigits) || '];'
WHEN ColumnType='TS' THEN ' TIMESTAMP;'
END
WHEN l_row=1 THEN
TRIM(columnname) || ': ' ||
CASE WHEN Nullable='Y' THEN 'nullable '
ELSE ''
END
||
CASE
WHEN ColumnType='CV' THEN ' string[max=' || TRIM(ColumnLength) || '];'
WHEN ColumnType='CF' THEN ' string[' || TRIM(ColumnLength) || '];'
WHEN ColumnType='D' THEN ' decimal[' || TRIM(DecimalTotalDigits) || ',' || TRIM(DecimalFractionalDigits) || '];'
WHEN ColumnType='TS' THEN ' TIMESTAMP;'
END || '0D'XC || ')'
END AS ds
, ROW_NUMBER () OVER(PARTITION BY tablename ORDER BY columnid) AS f_row
, ROW_NUMBER () OVER(PARTITION BY tablename ORDER BY columnid DESC) AS l_row
FROM
dbc.COLUMNS
WHERE
databasename='${databasename}'
AND
tablename='${tablename}'
) Q1
ORDER BY columnid
;
While searching I came acroos this
post, which has more detail on how to automate in DataStage. Thanks Jing.
Read more ...