DB2 需要帮助来更改表更改列集数据类型
我需要将列的数据类型从 VARCHAR
更改为 DECIMAL
我在列中有数据,但它们都是数值,如 10.00, 25.50
> 等等,
现在当我alter table MY_TABLE alter columns COL1 set data type todecimal(11,2)
时失败了。
我可以在这里遵循什么流程。
我确信这不是一个新问题,但我找不到解决方案,所以我不是绞尽脑汁,而是在这里问。
I need to change the data type of column from VARCHAR
to DECIMAL
I have data in the column, but they are all numeric values like 10.00, 25.50
etc,
Now when I do alter table MY_TABLE alter column COL1 set data type to decimal(11,2)
its failing.
Whats the process i can follow here.
I'm sure this is not a new question but i couldn't find the solution, so instead of raking my brains i'm asking out here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
语法是
ALTER TABLE {tableName} MODIFY {Column-Name} {New-Column-Definition}
IE
ALTER TABLE MY_TABLE MODIFY COL1 DECIMAL(11,2)
通常,这(更改列数据类型)不会将现有数据转换为新类型,并且可能会设置不兼容的值作为 NULL (或者它可能会进行智能转换,它几乎是 DBMS 服务器特定的)
如果数据量不太高,您可以执行类似的操作
编辑:第一部分可能不正确
The syntax is
ALTER TABLE {tableName} MODIFY {Column-Name} {New-Column-Definition}
I.E.
ALTER TABLE MY_TABLE MODIFY COL1 DECIMAL(11,2)
Generally this (altering the column data type) won't convert the existing data to the new type and will probably set the incompatible values as NULL (or it may make a smart conversion it's pretty much DBMS server specific)
You can do something like this if the data volume is not too high
Edit: The first part is probably incorrect
我假设 DB2 不允许您提供数据转换指南,但我向 SQL Server 诸神祈祷。
考虑到这一假设,添加一个新列,迁移数据,验证您的工作,删除原始列,重命名新列。
更改表 MY_TABLE 添加 col1_new 小数(11,2) NULL
更新我的表设置 col1_new = CAST(col1 AS 小数(11,2)
检查
col1
和col1_new
之间不同值、空值等的计数,并记住 < code>10.00、10.0
和10
在varchar
中是不同的值,在decimal
中是一个值确实如此。听起来这不是数据中的问题,但如果计数不同,则需要查找一些内容ALTER TABLE MY_TABLE DROP COLUMN col1
sp_rename
的 db2 等价物是什么I'm assuming DB2 won't allow you to provide a guide for the data conversion but I pray to the SQL Server gods.
Given that assumption, add a new column, migrate the data over, verify your work, drop the original column, rename the new.
ALTER TABLE MY_TABLE ADD col1_new decimal(11,2) NULL
UPDATE MY_TABLE SET col1_new = CAST(col1 AS decimal(11,2)
Check counts of distinct values, nulls etc between
col1
andcol1_new
bearing in mind10.00
,10.0
and10
would be different values invarchar
and one value indecimal
. It does not sound like that is an issue in your data but something to look for if the counts differALTER TABLE MY_TABLE DROP COLUMN col1
I have no idea what the db2 equivalent of
sp_rename
is尝试删除“SYSTOOLS”模式下的系统表。然后再次尝试更改它。
Try dropping the system tables under "SYSTOOLS" schema. Then try changing it again.
我尝试了@apokryfos 解决方案,它工作正常!
但它并不完整,一些 SQL 命令失败并出现错误
SQLCODE:-668,SQLSTATE:57016
示例:
SELECT count(COLUMN)
来自 SCHEMA.TABLE_NAME
其中 id = ID_NUMBER
仅获取前 1 行
这将失败!
要解决此问题,您需要重组表。打开 DB2 Shell,然后键入以下命令:
还有一个 REORG 的 SQL 命令,不过我没有尝试:
参考:
使用 SQLCODE 更新 db2 中的表失败: -668,SQLSTATE:57016,SQLERRMC:7;
I tried @apokryfos solution, it works fine!
It's not complete though, some SQL commands fails with error
SQLCODE: -668, SQLSTATE: 57016
Example:
SELECT count(COLUMN)
FROM SCHEMA.TABLE_NAME
WHERE id = ID_NUMBER
FETCH FIRST 1 ROWS ONLY
This will fail!
To solve this, you need to REORG the table. Open the DB2 Shell, and type the following commands:
There is also an SQL command to REORG, I didn't try it though:
Reference:
Failing update table in db2 with SQLCODE: -668, SQLSTATE: 57016, SQLERRMC: 7;