How to alter the datatype of a column in a table?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
CREATE TABLE dbo.MyTable (column_a INT );
GO
INSERT INTO dbo.MyTable (column_a) VALUES (10);
GO
ALTER TABLE dbo.MyTable ALTER COLUMN column_a DECIMAL (5, 2);
GO
DROP TABLE dbo.MyTable;
GO
回答2:
alter table yourtable alter column yourcolumnname yourdatatype;
This only works if SQL can cast the old values to the new values with the new datatype. If this fails, you will have to create a new column, migrate the data some way, and drop the old column.