use T-SQL to change table column schema

  • Hi,

    Is there any way to use T-SQl to change the column ORDER of a table.

    eg

    tblWronglyOrdered

    column3

    colum1

    column2

    I'd want the columns to be:

    colum1

    column2

    column3..

    I know that I can drop and then recreate the table with the correct columnOrder..but can T-SQL do the same?

    cheers,

    yogiberr

  • Why???

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

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

  • good question.

    some of the columns in the table are now less important than they were when I created them.so, when i go to QA, i'd like to return all the columns, but I'd like to see the columns that are most relevant first.

    yogi,

  • AFAIK there is no way using T-SQL.

    This might be interesting http://www.sqlservercentral.com/forum/link.asp?TOPIC_ID=12631

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

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

  • righto.

    Thanks Frank.

    yogi

  • If the order of the columns meant anything you could create a view and use that instead of the base table. Then you could re-arrange the view columns any time you would like.

    Dr. Peter Venkman: Generally you don't see that kind of behavior in a major appliance.

    Patrick

    Quand on parle du loup, on en voit la queue

  • never thought of that.

    nice one.

    yogi

  • Yes, there's a way!! Go into Enterprise manager, open the table in design view, move your column to where you want it be.

    Then click the "Save Change Script" icon on the toolbar. This will script out the change you just made. It's a lot of code, and basically, the table is being dropped and recreated and then the rows are inserted back into the table.

    Hope it helps!

    Bryan

  • Good to know.

    This way you can schedule it for off-peak hours.

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

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

  • quote:


    Yes, there's a way!! Go into Enterprise manager, open the table in design view, move your column to where you want it be.

    Then click the "Save Change Script" icon on the toolbar. This will script out the change you just made. It's a lot of code, and basically, the table is being dropped and recreated and then the rows are inserted back into the table.

    Hope it helps!

    Bryan


    Good answer, but the poster specifically said:

    I know that I can drop and then recreate the table with the correct columnOrder..but can T-SQL do the same?

    yogiberr,

    Do you still need the data in the 'old' columns? If not, just drop those columns. If you do need them, I think pbirch has the right idea.

    -SQLBill

  • Hi Bill,

    as it happens, I don't need the data that is in the old columns..still, it's good to get as many options as were suggested.

    thanks all.

    yogi.

Viewing 11 posts - 1 through 10 (of 10 total)

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