Home All Groups Group Topic Archive Search About

How can I insert a new field between existing field?

Author
10 Aug 2006 1:29 AM
Alex AU
Dear all,

Is there any way can I insert a new field between existing fields through TSQL or other means, without using Enterprise Manager?

Thanks a lot.



Regards,

Alex AU

Author
10 Aug 2006 1:48 AM
Hari Prasad
Hi,

With out dropping and recreating the table you can not insert a column in between 2 existing columns.
Actually enterprise manager internally does below events while inserting a new field....

1. Pull data out
2. Generate script of table and dependants
3. Drop and recreate the table with new structure
4. Load the data

So while you do a schema change on huge tables; it will result  in longer execution time...

Thanks
Hari
SQL Server MVP

  "Alex AU" <ac***@msn.com> wrote in message news:5003A40C-1603-4C8F-B591-DFFF69DFC567@microsoft.com...
  Dear all,

  Is there any way can I insert a new field between existing fields through TSQL or other means, without using Enterprise Manager?

  Thanks a lot.



  Regards,

  Alex AU
Author
10 Aug 2006 5:09 AM
Alex AU
Thanks Hari,

If I want to mimic what Enterprise Manager do, what will be the right way?

Can I do as follows:-
- Create a temp table with the new fields inserted
- insert the data from old table to temp table
- drop the old table
- Create the table with the new structure
- insert the data back from temp table
- drop the temp table

Can I rename the temp table to the original table name to replace the last 3 steps?

Thanks a lot.

Regards,

Alex AU
  "Hari Prasad" <hari_prasa***@hotmail.com> wrote in message news:uweZCACvGHA.2260@TK2MSFTNGP03.phx.gbl...
  Hi,

  With out dropping and recreating the table you can not insert a column in between 2 existing columns.
  Actually enterprise manager internally does below events while inserting a new field....

  1. Pull data out
  2. Generate script of table and dependants
  3. Drop and recreate the table with new structure
  4. Load the data

  So while you do a schema change on huge tables; it will result  in longer execution time...

  Thanks
  Hari
  SQL Server MVP

    "Alex AU" <ac***@msn.com> wrote in message news:5003A40C-1603-4C8F-B591-DFFF69DFC567@microsoft.com...
    Dear all,

    Is there any way can I insert a new field between existing fields through TSQL or other means, without using Enterprise Manager?

    Thanks a lot.



    Regards,

    Alex AU
Author
10 Aug 2006 1:50 PM
Paul Ibison
Unless you're really lucky, it's far more complicated that than. You'll have to take account of all constraints - Defaults, FKs etc and all indexes. These need to be dropped from the existing table and later readded to the tmp table. Also keep an eye out for any views created using select * from tablename, as they'll need to be refreshed afterwards. Finally, I'd also question whether it's worth it, when there's such little gain. Client apps should access columns by name rather than position, so typically the main gain is just neatness for the DBA.
   Cheers,
        Paul Ibison SQL Server MVP, www.replicationanswers.com
Author
11 Aug 2006 10:52 PM
Mike C#
Paul Ibison SQL Server MVP said:
> "Client apps should access columns by name rather than position, so
> typically the main gain is just neatness for the DBA."

....Unless you're trying to do something completely off-the-wall and "crazy"
like writing a custom application to Bulk Load data into your table in which
case you have to specify columns by ordinal position...
Author
15 Aug 2006 6:00 AM
Arnie Rowland
Then why not create a VIEW with the fields in the necessary order, and bulk
load the VIEW?

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

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


Show quote
"Mike C#" <x**@xyz.com> wrote in message
news:4X7Dg.6916$5L4.3133@newsfe10.lga...
> Paul Ibison SQL Server MVP said:
>> "Client apps should access columns by name rather than position, so
>> typically the main gain is just neatness for the DBA."
>
> ...Unless you're trying to do something completely off-the-wall and
> "crazy" like writing a custom application to Bulk Load data into your
> table in which case you have to specify columns by ordinal position...
>
Author
16 Aug 2006 11:00 PM
Mike C#
"Arnie Rowland" <ar***@1568.com> wrote in message
news:OiQESADwGHA.4460@TK2MSFTNGP05.phx.gbl...
> Then why not create a VIEW with the fields in the necessary order, and
> bulk load the VIEW?

As long as the columns in the view are in the correct ordinal positions...
Table or view, bulk operations API's reference columns by ordinal position.
Author
10 Aug 2006 2:12 AM
Arnie Rowland
The actual field order 'should' not matter. If you add a new column and it is at the 'end of the list', you can always retrieve the list of columns as you want -with the new column in the 'middle'. Of course, that gets in the way of using 'SELECT *' -but you shouldn't be doing that anyway!

(There some considerations that SQL Server will take in regard to field/datatype placement in the row on the datapage but that should not interfere with your getting the data in the fashion you desire.)

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

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


  "Alex AU" <ac***@msn.com> wrote in message news:5003A40C-1603-4C8F-B591-DFFF69DFC567@microsoft.com...
  Dear all,

  Is there any way can I insert a new field between existing fields through TSQL or other means, without using Enterprise Manager?

  Thanks a lot.



  Regards,

  Alex AU
Author
10 Aug 2006 12:43 PM
Tracy McKibben
Alex AU wrote:
> Dear all,

> Is there any way can I insert a new field between existing fields
> through TSQL or other means, without using Enterprise Manager?

> Thanks a lot.



> Regards,

> Alex AU

Field order is irrelevant.  Given two tables:

TableA:  RowID, Value, Description, UpdateDate

TableB:  RowID, Value, UpdateDate, Description

The field order means nothing, because your queries should always
specify a field list:

SELECT Value, Description, UpdateDate
FROM TableA
WHERE RowID = 1

SELECT Value, Description, UpdateDate
FROM TableB
WHERE RowID = 1


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com

AddThis Social Bookmark Button