adding a column in the middle of the table

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
Who is Participating?
Anthony PerkinsConnect With a Mentor Commented:
>>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.
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.
hp746Author Commented:
can't i achive using script
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

lcohanDatabase AnalystCommented:
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.

lcohanDatabase AnalystCommented:
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.
hp746Author Commented:
with out designer how can i do it from script?
Anthony PerkinsConnect With a Mentor Commented:
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

derekkrommConnect With a Mentor Commented:
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.

lcohanConnect With a Mentor Database AnalystCommented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.