Anand
Anand

Reputation: 21320

Dropping the same column name from mutiple tables in Oracle

I have to delete a same column present in many tables. for e.g. column with name say 'c' is present in many tables..t1,t2,t3...

One solution is to write as many ALTER statements to drop a column as the number of tables having that column.

is there any way where I can do the same thing using single ALTER table statement.

Upvotes: 3

Views: 660

Answers (1)

user672739
user672739

Reputation:

No. An ALTER TABLE statement can not alter more than one table at a time. You could write some dynamic SQL based on ALL_TAB_COLS e.g.

SELECT 'ALTER TABLE ' || owner || '.' || table_name || ' DROP COLUMN '|| column_name || ';'
FROM all_tab_columns
WHERE column_name = 'MY_UNWANTED_COLUMN'
AND owner = 'MY_OWNER'
/

then run that script. You might want to add

AND table_name IN ('MY_TAB1','MY_TAB2')

to specify an exact list of tables for extra piece of mind.

Upvotes: 3

Related Questions