Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify multiple columns in single Alter Table query [duplicate]

Is there a way to modify multiple columns in one sql query? For example I want to change the column definition of multiple columns in single table as follows. I am using SQL server 2012.

ALTER TABLE [Department]     
ALTER COLUMN [DepartmentName] VARCHAR(200) NULL ,
[ProjectManagerName] VARCHAR(200) NULL ,
[AccountManagerName] VARCHAR(200) NULL 
like image 328
SPKan Avatar asked May 11 '16 12:05

SPKan


People also ask

How do you add multiple columns in a single ALTER statement?

First, you specify the table name after the ALTER TABLE clause. Second, you put the new column and its definition after the ADD COLUMN clause. Note that COLUMN keyword is optional so you can omit it. Third, MySQL allows you to add the new column as the first column of the table by specifying the FIRST keyword.

Can we modify more than one column using a single ALTER TABLE?

It is not possible to do multiple ALTER column for a table.

Can we modify multiple columns in SQL?

We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required.

How to add multiple columns in mysql table using ALTER TABLE?

Let’s see how to add multiple columns in MySQL table using the Alter Table statement. This ALTER TABLE example will modify two columns to the contacts table – last_name and first_name. The last_name field will be changed to a varchar (55) NULL column and will appear after the contact_type column in the table.

How to alter multiple columns at once in SQL Server?

How to Alter Multiple Columns at Once in SQL Server? In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this.

How to modify a column of a table in SQL Server?

Summary: in this tutorial, you will learn how to use the SQL Server ALTER TABLE ALTER COLUMN statement to modify a column of a table. SQL Server allows you to perform the following changes to an existing column of a table: To modify the data type of a column, you use the following statement:

How to modify multiple columns to the same datatype?

If you want to modify all or several of the columns in your table to the same datatype (such as expanding a VARCHAR field from 50 to 100 chars), you can generate all the statements automatically using the query below. This technique is also useful if you want to replace the same character in multiple fields (such as removing from all columns).


2 Answers

Alter multiple columns in one time - impossible.

You could create Temp table with your edited columns, copy data from source table to temp table, drop your source table and rename temp table.

like image 73
Stanislovas Kalašnikovas Avatar answered Nov 13 '22 11:11

Stanislovas Kalašnikovas


It is not possible to do multiple ALTER column for a table.

You have to alter them one by one like

ALTER TABLE Department ALTER COLUMN [DepartmentName] VARCHAR(200) NULL;
ALTER TABLE Department ALTER COLUMN [ProjectManagerName] VARCHAR(200) NULL;
ALTER TABLE Department ALTER COLUMN [ProjectManagerName] VARCHAR(200) NULL;
ALTER TABLE Department ALTER COLUMN [AccountManagerName] VARCHAR(200) NULL; 
like image 40
Rahul Tripathi Avatar answered Nov 13 '22 11:11

Rahul Tripathi