Solved

how to add a column in certain positon in table?

Posted on 2010-11-16
9
348 Views
Last Modified: 2012-08-14
Hi Expert,

How I can add a cloumn to a exist table in certain position for instance,

Table 1

FirstName varchar(20), Lastname varchar(20).

I want add a column MiddleName varchar(20) in second column and all data can not be lost.

Thanks in advance.
0
Comment
Question by:dshi15
9 Comments
 
LVL 20

Accepted Solution

by:
DVation191 earned 100 total points
ID: 34145778
ALTER TABLE table_name ADD column_name column-definition [ FIRST | AFTER col_name ]

e.g.
Alter Table Stu_Table add Stu_Class int(10) AFTER Stu_Name

http://www.roseindia.net/sql/sql-add-column-position.shtml
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 34145782
in short: this is not possible unless you recreate the table.
of course, you can rename the table, create new table with proper design, and insert back the data.
when you use the SQL server GUI, you can do that in the design mode, and the GUI will build all the SQL for you.

now, the position of the column should not matter, at least not with serious development, because the database, in the end, should be nothing else than data storage and eventually (part of) the data layer.
0
 
LVL 9

Assisted Solution

by:s_chilkury
s_chilkury earned 100 total points
ID: 34145798
Go to the design view of your table, then right click on
the position where you want the new table and select insert column.
0
 
LVL 16

Assisted Solution

by:EvilPostIt
EvilPostIt earned 100 total points
ID: 34145903
As a side note, if you use the designer to change the ordinal position of the column. Behind the scenes it re-creates the table.

Best option is to follow what angelIII has suggested.
I think DVation191's comment applies to MySQL
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:dshi15
ID: 34145986
I need use sql statement, not design view. Could some one write sql statement? How to recreate?

thanks,
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34146008
You gonna need to post the table structure first.
0
 
LVL 23

Assisted Solution

by:Racim BOUDJAKDJI
Racim BOUDJAKDJI earned 100 total points
ID: 34151001
Two things to take into consideration:

> There is no obligation to have the same column order in the table as you'd have in a display.
> Appart from amount of space used and under the assumption of relevant indexing, order of columns is totally irrelevant in a database.  You can create *ANY* order you need in a view.

This is one of the typical examples where the finality does not worth the effort of reordering the columns...

Hope this helps
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34153193
I Agree.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34160730
>>Could some one write sql statement? How to recreate?<<
If you persist and in case you missed the point, here it goes again:
Use SSMS to make the change but do not save instead copy the script generated and use that in your code.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now