adding a column in the middle of the table

Posted on 2011-02-24
Last Modified: 2012-08-13
How can i add a column  in the middle  of the table through script?

example:i have table  testable,it has columns col1,col2
when i add col3 it shoud go in the middle like col1,col3,col2
Question by:hp746
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2

Expert Comment

ID: 34972928
This can ONLY be done by re-creating the table again.


Create View as an outcome in the preferred selection order say col1, col3, col2

This will solve your purpose.

Author Comment

ID: 34973563
can't i achive using script
LVL 40

Expert Comment

ID: 34974724
IF and ONLY IF the table is reasonable small AND not accessed live online then you could use SQL Management Studio, Right click table, select Design, then on the column list click one column you want to insert before then right click that column you will see a menu pop-up where you can select "Insert Column" before the one selected however...if you generate the SQL Script you will see that the table is actually re-created.

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

LVL 40

Expert Comment

ID: 34974742
And obviously... on the window above after you made all your changes but BEFORE click OK you have the option to "Generate Change Script" that you can save and execute later.

Author Comment

ID: 34975277
with out designer how can i do it from script?
LVL 75

Accepted Solution

Anthony Perkins earned 250 total points
ID: 34976617
>>with out designer how can i do it from script? <<
You have to:
1. Create the new table with the correct structure.
2. Copy the data from the old table.
3. Drop the old table.
4. Rename the new table to the old name.

This of course assumes there are not foreign constraints, otherwise you would have to drop those first and then re-create them.
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 250 total points
ID: 34976636
So to answer your specific question asking for a script it would look like this:
-- 1.
CREATE TABLE dbo.Tmp_testable (
	col1 int NULL,			-- Change data types as neceesary
	col3 int NULL,
	col2 int NULL)
-- 2.
INSERT dbo.Tmp_testable (col1, col2)
SELECT col1, col2 FROM dbo.testable 
-- 3.
DROP TABLE dbo.testable
-- 4.
EXEC sp_rename N'dbo.Tmp_testable', N'testable', 'OBJECT'

Open in new window

LVL 15

Assisted Solution

derekkromm earned 125 total points
ID: 34976946
You could technically update the system tables that store the column metadata, but it is highly recommended that you don't go that route. The ordering of the columns shouldn't matter.

LVL 40

Assisted Solution

lcohan earned 125 total points
ID: 34981250
Since Microsoft introduced DM views in SQL, system tables are hidden and this is not even posible not just NOT RECOMMENDED however with the UI design mode you do all you want then easily  generate the script as I described above.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question