且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不丢失数据的情况下更改 SQL 数据库中的列数据类型

更新时间:2023-02-03 22:33:34

您可以使用以下命令轻松完成此操作.任何值 0 都会变成 0(BIT = false),其他任何值都会变成 1(BIT = true).

You can easily do this using the following command. Any value of 0 will be turned into a 0 (BIT = false), anything else will be turned into 1 (BIT = true).

ALTER TABLE dbo.YourTable
   ALTER COLUMN YourColumnName BIT

另一种选择是创建一个 BIT 类型的新列,从旧列填充它,完成后,删除旧列并将新列重命名为旧列姓名.这样,如果转换过程中出现问题,您可以随时返回,因为您仍然拥有所有数据.

The other option would be to create a new column of type BIT, fill it from the old column, and once you're done, drop the old column and rename the new one to the old name. That way, if something during the conversion goes wrong, you can always go back since you still have all the data..