如何将 SQL 表中列的数据类型从整数更改为小数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27175089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change data type of a column in an SQL table from integer to decimal
提问by user3115933
I have assigned the data type of one of the columns of a table I have created as int
. My problem is that it is not showing decimal places when I run a query against it.
我已将我创建的表的其中一列的数据类型指定为int
. 我的问题是当我对它运行查询时它没有显示小数位。
How do I correct the data type of this column so that it accepts decimal places?
如何更正此列的数据类型以使其接受小数位?
Table is dbo.Budget
and the column concerned is called ROE
.
表是dbo.Budget
,相关的列被称为ROE
。
回答by marc_s
Easy - just run this SQL statement
简单 - 只需运行此 SQL 语句
ALTER TABLE dbo.Budget
ALTER COLUMN ROE DECIMAL(20,2) -- or whatever precision and scale you need.....
See the freely availableMSDN documentationon what exactly the precision, scale and length in decimal numbers are and what ranges of values are possible
请参阅免费提供的MSDN 文档,了解十进制数的精确度、小数位数和长度究竟是什么以及可能的值范围
回答by Solomon Rutzky
Just to have this stated clearly:
只是为了清楚地说明这一点:
If there is no data in the table, or not that much, then the simple ALTER TABLE
statement (as described in the other answers here) is fine.
如果表中没有数据,或者没有那么多数据,那么简单的ALTER TABLE
语句(如此处的其他答案中所述)就可以了。
But, if there is a lot of data (millions of rows, or possibly less depending on the size of the table) and/or a lot of contention on the table and not much opportunity for a full downtime / maintenance window, then it requires a different approach, such as what I described in this answer: Narrowing the Data Types on a very large table.
但是,如果表中有大量数据(数百万行,或者可能更少,具体取决于表的大小)和/或表上存在大量争用,并且完全停机/维护窗口的机会不多,则需要一种不同的方法,例如我在这个答案中所描述的:Narrowing the Data Types on a very large table。
回答by saktiprasad swain
Alter datatype of that column ..But In general sql wont allow to channge.It will prompt u drop that column..There is setting to achive that thing.
Go to Tool-Option-designers-Table and Database designers and Uncheck Prevent saving option.I m taking abt sql server 2008R2
更改该列的数据类型..但一般情况下,sql 不允许更改。它会提示您删除该列..有设置可以实现该功能。
转到工具-选项-设计器-表和数据库设计器并取消选中阻止保存选项。我正在使用 abt sql server 2008R2
回答by HaveNoDisplayName
You can execute this simple sql statement
你可以执行这个简单的sql语句
Alter table yourtable
Alter Column yourtable_column Decimal(10,2)
you can set decimal precision and scale whatever you need.
您可以设置十进制精度并根据需要进行缩放。