How to change a column without dropping a table in SQL 2008

前端 未结 5 378
执笔经年
执笔经年 2021-01-31 02:01

Why does SQL 2008 all of a sudden want to drop my tables when I go to change the column type from say int to real? This never happened in SQL 2005 to my knowledge. Any insight

5条回答
  •  广开言路
    2021-01-31 02:49

    I can't believe the top answer has been sitting here for so long - it is very dangerous advice!

    There are few operations that you can do inplace without dropping your table:

    • Expand a varchar column https://dba.stackexchange.com/questions/5211/changing-column-width
    • Make a column nullable (but not vice-versa)
    • Renaming columns using sp_rename

    If you find yourself in the situation where altering a column is not possible without dropping the table, you can usually use a SELECT INTO query to project your data into a new table, then drop the old table (temporarily disabling constraints) and then renaming the projected table. You will need to take your database offline for maintenance in this case though.

提交回复
热议问题