在sqlserver 2008中将一种数据类型更改为另一种数据类型 [英] to change one datatype to another datatype in sqlserver 2008

查看:152
本文介绍了在sqlserver 2008中将一种数据类型更改为另一种数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经创建了一个包含3行的表格...

Hi

i have created a table with 3 rows...

user_id   int,
user_name varchar(50),
address   varchar(50),
amount    bigint



user_id是主键.

我已在表格中插入10个值.
现在我想将数量datatype(bigint)更改为float ..当我尝试执行此操作时出现错误...任何人都可以为我找到解决方案吗?
在此先感谢

问候
Meenakshi



user_id is primary key.

i have inserted 10 values into table .
Now i want to change amount datatype(bigint) to float..when i try to do this im getting error...can any one get me the solution for this?
Thanks in advance

Regards
Meenakshi

推荐答案

您不能直接更改列的数据类型.您必须分几个阶段进行操作:

1-用正确的数据类型创建另一列
You cannot change the datatype of your column directly. You have to do it in several stages :

1- Create another column with the correct datatype
ALTER TABLE <yourtable> ADD COLUMN newcolumn float



2-将数据从第一列复制到新列中.



2- Copy the data from the first column to the new one

UPDATE <yourtable> SET newcolumn = CONVERT(amount, float)


(确保数量列中没有任何值超过浮点数据类型的值)

3-删除金额列


(make sure there are not any value in amount column that exceeds the float datatype boudaries)

3- Delete the amount column

ALTER TABLE <yourtable> DELETE COLUMN amount



4-重命名mycolumn为数量(您必须搜索它-有一个存储过程,但是我不记得它的名字了)

希望对您有所帮助.



4- Rename the mycolumn to amount (you''ll have to search for this - there is a stored procedure, but I can''t remember its name)

Hope this helps.


要澄清一点,基本上您可以更改列的数据类型,请参见
To clarify a bit, basically you can change the data type of a column, see ALTER TABLE[^] and especially ALTER COLUMN. There are restrictions like:
- the column may not be certain data type
- implicit conversion must exist
- it may not be part of a constraint and so on

Now the bigint isn''t implicitly convertible to float (AFAIK) so this could be the cause to your problem. Recreating the column would be the solution as posted by phil.o.


您可以发布错误.这样问题就更明显了.
can u post the error. so that problem can be much more visible.


这篇关于在sqlserver 2008中将一种数据类型更改为另一种数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆