Home All Groups Group Topic Archive Search About

Alter Table ....Add Column

Author
18 Aug 2006 2:14 PM
Bob
Most everyone knows how to alter a table and add a column...or else we
can look it up in BOL.

But....how do I add a column WHEN I want the column to be the xth
column in the table.

So if I have

Create table Experiment (id int,fname varchar(15),lname varchar(15))

Then I want to

alter table Experiment Add SSnumber char(9)

But I want the SSnumber field to be the second field....not the 4th as
it will appear.

Thanks-In-Advance

Author
18 Aug 2006 2:21 PM
Aaron Bertrand [SQL Server MVP]
You drop the table and re-create it.



Show quote
"Bob" <Go1***@Yahoo.Com> wrote in message
news:1155910457.450876.327340@b28g2000cwb.googlegroups.com...
>
> Most everyone knows how to alter a table and add a column...or else we
> can look it up in BOL.
>
> But....how do I add a column WHEN I want the column to be the xth
> column in the table.
>
> So if I have
>
> Create table Experiment (id int,fname varchar(15),lname varchar(15))
>
> Then I want to
>
> alter table Experiment Add SSnumber char(9)
>
> But I want the SSnumber field to be the second field....not the 4th as
> it will appear.
>
> Thanks-In-Advance
>
Author
18 Aug 2006 2:55 PM
Bob
Aaron, then I will lose all my data.....of course I know I could just
create the new table...populate it with the old data...then drop the
original table and re-name the new table...I suppose that's the best
solution.

Aaron Bertrand [SQL Server MVP] wrote:

Show quote
> You drop the table and re-create it.
>
>
>
> "Bob" <Go1***@Yahoo.Com> wrote in message
> news:1155910457.450876.327340@b28g2000cwb.googlegroups.com...
> >
> > Most everyone knows how to alter a table and add a column...or else we
> > can look it up in BOL.
> >
> > But....how do I add a column WHEN I want the column to be the xth
> > column in the table.
> >
> > So if I have
> >
> > Create table Experiment (id int,fname varchar(15),lname varchar(15))
> >
> > Then I want to
> >
> > alter table Experiment Add SSnumber char(9)
> >
> > But I want the SSnumber field to be the second field....not the 4th as
> > it will appear.
> >
> > Thanks-In-Advance
> >
Author
18 Aug 2006 3:38 PM
Aaron Bertrand [SQL Server MVP]
> Aaron, then I will lose all my data.....of course I know I could just
> create the new table...populate it with the old data...then drop the
> original table and re-name the new table...I suppose that's the best
> solution.

Or, not messing with column order, and just adding the column at the "end"
of the table.  Can you explain why column order is important in this case?
Author
18 Aug 2006 2:26 PM
Dbers
I agree, I think you must recreate the table.  When I dont know how
something is done I usually do it through the GUI and run profiler to
see how management studio (or EM) handles it.  In this case it creates
a temp table with the new column order and fills it with the data,
drops the original table, renames the temp table to the original name
and then throws the primary key back on. 

PT
Author
18 Aug 2006 2:30 PM
Aaron Bertrand [SQL Server MVP]
>I agree, I think you must recreate the table.  When I dont know how
> something is done I usually do it through the GUI and run profiler to
> see how management studio (or EM) handles it.  In this case it creates
> a temp table with the new column order and fills it with the data,
> drops the original table, renames the temp table to the original name
> and then throws the primary key back on.

Which of course, on a large table and/or heavy volume system, could be an
absolute catastrophe.
Author
18 Aug 2006 3:50 PM
Tracy McKibben
Bob wrote:
Show quote
> Most everyone knows how to alter a table and add a column...or else we
> can look it up in BOL.
>
> But....how do I add a column WHEN I want the column to be the xth
> column in the table.
>
> So if I have
>
> Create table Experiment (id int,fname varchar(15),lname varchar(15))
>
> Then I want to
>
> alter table Experiment Add SSnumber char(9)
>
> But I want the SSnumber field to be the second field....not the 4th as
> it will appear.
>
> Thanks-In-Advance
>

Column order is irrelevant.  You're going to retrieve data from the
table using a SELECT statement, simply specify the fields there in the
order that you want them retrieved.

Columns in table:
    Name
    Date
    SSN

Columns in select:
    SELECT Name, SSN, Date FROM table



--
Tracy McKibben
MCDBA
http://www.realsqlguy.com
Author
19 Aug 2006 2:06 AM
Arnie Rowland
You could just add a column, which is then at the end of the schema, AND
then create a VIEW with the columns in the order that you desire and use the
VIEW if it is too much trouble to specify the column list in the query
(which, by the way, is the [BEST PRACTICE]).

Don't use 'SELECT *' -it's signifies laziness!

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


Show quote
"Bob" <Go1***@Yahoo.Com> wrote in message
news:1155910457.450876.327340@b28g2000cwb.googlegroups.com...
>
> Most everyone knows how to alter a table and add a column...or else we
> can look it up in BOL.
>
> But....how do I add a column WHEN I want the column to be the xth
> column in the table.
>
> So if I have
>
> Create table Experiment (id int,fname varchar(15),lname varchar(15))
>
> Then I want to
>
> alter table Experiment Add SSnumber char(9)
>
> But I want the SSnumber field to be the second field....not the 4th as
> it will appear.
>
> Thanks-In-Advance
>

AddThis Social Bookmark Button