Can I logically reorder columns in a table?

后端 未结 8 1555
抹茶落季
抹茶落季 2020-12-15 02:04

If I\'m adding a column to a table in Microsoft SQL Server, can I control where the column is displayed logically in queries?

I don\'t want to mess with the physical

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 02:42

    1. Script your existing table to a query window.
    2. Run this script against a Test database (remove the Use statement)
    3. Use SSMS to make the column changes you need
    4. Click Generate Change Script (left most and bottommost icon on the buttonbar, by default)
    5. Use this script against your real table

    All the script really does is create a second table table with the desired column orders, copies all your data into it, drops the original table and then renames the secondary table to take its place. This does save you writing it yourself though should you want a deploy script.

提交回复
热议问题