Add field to the middle - SQL Rebuilt table?

  • Hi, I have one question. If I want to add new field in between other existing fields, does SQL Server needs to rebuild the table?

    For example:

    Col1,

    Col2,

    Col3

    I want to change the table's structure as:

    Col1,

    ColAA,

    Col2,

    Col3.

    Thanks!

  • Why? There is no real need for this!

    However, you can open your table in design view in EM, insert an empty row at the desired place, define data type, save and you're done. Behind the scenes I think the table is dropped and rebuilt, so depending on how many rows are in the table it might take time and blow up you log file.

    Frank

    http://www.insidesql.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Dosen't referential integrity come into play here? If the table you are going to modify has foreign key constraints related to it, won't referential integrity prevent the table from being dropped before it can be rebuilt with the new column?

  • Try it out.

    Works here without any issue. Even with having a relationship

    Frank

    http://www.insidesql.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • A way to do this without reloading (which enterprise manager does behinds the scenes including constraints) is to add the column, use sp_rename on the table and then create a view with the original table name.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply