Preserve SQL Indexes While Altering Column Datatype

后端 未结 4 1992
既然无缘
既然无缘 2020-12-16 13:26

I have a smalldatetime column that I need to alter to be a datetime column. This is something that will be part of an install process, so it cannot be a manual procedure. Un

4条回答
  •  渐次进展
    2020-12-16 14:07

    EDIT: It depends on the original and changed datatype. If you try to alter a column from varchar to nvarchar, it will fail. Whereas, if you alter column from varchar(16) to varchar(32), it will succeed.

    --Disable Index
    ALTER INDEX MyIndex ON MyTable DISABLE
    GO
    
    -- Change column datatype
    
    --Enable Index
    ALTER INDEX MyIndex ON MyTable REBUILD
    GO
    

    If you change the type of a column, then all indexes that use that column will have to be rebuilt.

    But unless you have huge volumes of data (or run 24/7), rebuilding indexes is no big deal. Just schedule a maintenance window.

提交回复
热议问题