Add a column to a DB2/400 table with a specific ordinal position
-
22-09-2019 - |
Question
Is there an SQL command on the AS400/iSeries/System-i/whatever to add a column to a table in a specific ordinal position, or moving an existing column to a different position?
Solution
IBM i 7.1 now allows you to add a column in front of another.
ALTER TABLE table ADD COLUMN colname ... BEFORE othercolumn
OTHER TIPS
No. The ALTER TABLE statement will allow you add a column to a table, but, according to the documentation:
The new column is the last column of the table; that is, if initially there are n columns, the added column is column n+1.
If you'd like to change the order of columns in your table, your best bet is to:
- Use the RENAME statement to rename the table.
- Recreate the table, with its original name, with the columns in the order that you want.
- Use an INSERT SELECT to populate the new table with the data from the renamed table.
- When you're sure the data is intact, you can drop the renamed version of the table.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow