Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a column to specific position in MSSQL Server

Tags:

sql-server

ALTER TABLE Log ADD log_id bigint IDENTITY BEFORE cust_id_fk

The above code adds a new column to last position. I want it to be added to the first position. Also I want to make it as Primary Key.

like image 872
Akhil K Nambiar Avatar asked Oct 26 '11 07:10

Akhil K Nambiar


People also ask

Can we specify the position where a new column has to be added?

Answer. Yes, you can add a new column in a specified position into a dataframe, by specifying an index and using the insert() function. By default, adding a column will always add it as the last column of a dataframe. This will insert the column at index 2, and fill it with the data provided by data .

How do I add a column to the first position in SQL Server?

How do I add a column to the first position in SQL Server? 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. Third, MySQL allows you to add the new column as the first column of the table by specifying the FIRST keyword.

How do I add a column to a SQL table after a specific column?

In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. 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.

How do I change the position of a column in SQL?

Using SQL Server Management StudioIn Object Explorer, right-click the table with columns you want to reorder and select Design. Select the box to the left of the column name that you want to reorder. Drag the column to another location within the table.


2 Answers

You would need to drop the table and recreate it with the columns in the correct order. If you make the table changes in SSMS then it can generate a change script for you which you could then use to deploy the change to a production server.

like image 86
Ira Rainey Avatar answered Oct 06 '22 01:10

Ira Rainey


Even if the question is old, a more accurate about Management Studio would be required.

You can create the column manually or with Management Studio. But Management Studio will require to recreate the table and will result in a time out if you have too much data in it already, avoid unless the table is light.

To change the order of the columns you simply need to move them around in Management Studio. This should not require (Exceptions most likely exists) that Management Studio to recreate the table since it most likely change the ordination of the columns in the table definitions.

I've done it this way on numerous occasion with tables that I could not add columns with the GUI because of the data in them. Then moved the columns around with the GUI of Management Studio and simply saved them.

You will go from an assured time out to a few seconds of waiting.

like image 27
Rv3 Avatar answered Oct 05 '22 23:10

Rv3