Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to DROP multiple columns with a single ALTER TABLE statement in SQL Server?

I would like to write a single SQL command to drop multiple columns from a single table in one ALTER TABLE statement.

From MSDN's ALTER TABLE documentation...

DROP { [CONSTRAINT] constraint_name | COLUMN column_name } 

Specifies that constraint_name or column_name is removed from the table. DROP COLUMN is not allowed if the compatibility level is 65 or earlier. Multiple columns and constraints can be listed.

It says that mutliple columns can be listed in the the statement but the syntax doesn't show an optional comma or anything that would even hint at the syntax.

How should I write my SQL to drop multiple columns in one statement (if possible)?

like image 473
Jesse Webb Avatar asked Jun 14 '11 15:06

Jesse Webb


People also ask

Can we drop multiple columns from a table in SQL?

Multiple columns can be deleted from the MySQL table using a single ALTER statement by separating the DROP keyword and column name list with a comma.

How do I drop multiple columns at a time in SQL?

Multiple columns can be dropped with a single query by simply comma separating a list of DROP COLUMN statments. To drop both the "foo" and "bar" columns from the "test" table do this: ALTER TABLE test DROP COLUMN foo, DROP COLUMN bar; As many additional columns can be added to the list as required.

Can we drop column using ALTER?

ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table.


1 Answers

For SQL Server:

ALTER TABLE TableName     DROP COLUMN Column1, Column2; 

The syntax is

DROP { [ CONSTRAINT ] constraint_name | COLUMN column } [ ,...n ]  

For MySQL:

ALTER TABLE TableName     DROP COLUMN Column1,     DROP COLUMN Column2; 

or like this1:

ALTER TABLE TableName     DROP Column1,     DROP Column2; 

1 The word COLUMN is optional and can be omitted, except for RENAME COLUMN (to distinguish a column-renaming operation from the RENAME table-renaming operation). More info here.

like image 122
Alex Aza Avatar answered Sep 22 '22 14:09

Alex Aza