How do I alter the precision of a decimal column in Sql Server?

前端 未结 5 784
野性不改
野性不改 2020-12-25 09:18

Is there a way to alter the precision of an existing decimal column in Sql Server?

5条回答
  •  自闭症患者
    2020-12-25 10:08

    There may be a better way, but you can always copy the column into a new column, drop it and rename the new column back to the name of the first column.

    to wit:

    ALTER TABLE MyTable ADD NewColumnName DECIMAL(16, 2);
    GO
    
    UPDATE  MyTable
    SET     NewColumnName = OldColumnName;
    GO
    
    ALTER TABLE CONTRACTS DROP COLUMN OldColumnName;
    GO
    
    
    EXEC sp_rename
        @objname = 'MyTable.NewColumnName',
        @newname = 'OldColumnName',
        @objtype = 'COLUMN'
    GO
    

    This was tested on SQL Server 2008 R2, but should work on SQL Server 2000+.

提交回复
热议问题