如何使用 DB2 SQL 将 DECFLOAT 更改为 INT?
How do you change a DECFLOAT to an INT using DB2 SQL?
简单地说,我正试图摆脱我数据库中的所有这些 DECFLOAT 类型,因为很多报告工具(例如 Tableau)不想使用它们。我是 DB2 的新手,并且在网上找到了很多关于从任何数据类型 -> DECFLOAT 开始的文献,但是没有关于从 DECFLOAT -> INT 开始的实质内容。
这可能吗?有什么建议吗?
根据 Table 2. Supported casts between built-in data types 支持将 DECFLOAT 转换为整数,实际上您可以转换为以下任何一种:SMALLINT、INTEGER、BIGINT、DECIMAL、DECFLOAT、REAL、DOUBLE、CHAR、VARCHAR(取决于数据,例如 smallint 的大小是有限制的)。
但是为什么要将 table 列更改为整数?为什么不用 DECIMAL 呢?或者,不要改变您的 tables 以适应 BI 产品,而是使用转换为十进制的视图。您还可以通过视图控制舍入。
虽然今天的 BI 工具可能会遇到这种数据类型的问题,但该数据类型是基于标准 EEE754r 的,在未来版本的 BI 工具中它可能不会成为问题。也许在更改任何 table 之前阅读 DECFLOAT: The data type of the future。
我同意 @Used_by_Already 的观点,您最好更新您的 BI 工具,以便它们与 DECFLOAT
一起使用,或者使用 VIEW
s 转换为 INT
或DECIMAL
(甚至FLOAT
!?)在飞行中。
但是如果您确实想要更改现有 table 的数据类型,您可以这样做(有一些限制),如下例所示
create table df(df decfloat(16) not null primary key) organize by row;
insert into df values 1.0, 2.00, 3.120;
select * from df;
给予
DF
------------------------
1.0
2.00
3.120
3 record(s) selected.
然后
alter table df alter column df set data type integer;
returns
SQL0478N The statement failed because one or more dependencies exist on the
target object. Target object type: "COLUMN". Name of an object that is
dependent on the target object: "SQL180924130131030". Type of object that is
dependent on the target object: "PRIMARY KEY". SQLSTATE=42893
所以
alter table df drop primary key;
alter table df alter column df set data type integer;
然后
select * from df
给予
DF
-----------
1
2
3
3 record(s) selected.
但是
insert into df values 4
由于重组挂起而失败
SQL0668N Operation not allowed for reason code "7" on table "PAUL.DF".
SQLSTATE=57016
所以
call admin_cmd('reorg table df');
insert into df values 4;
alter table df add primary key (df);
select * from df;
给予
DF
-----------
1
2
3
4
4 record(s) selected.
简单地说,我正试图摆脱我数据库中的所有这些 DECFLOAT 类型,因为很多报告工具(例如 Tableau)不想使用它们。我是 DB2 的新手,并且在网上找到了很多关于从任何数据类型 -> DECFLOAT 开始的文献,但是没有关于从 DECFLOAT -> INT 开始的实质内容。
这可能吗?有什么建议吗?
根据 Table 2. Supported casts between built-in data types 支持将 DECFLOAT 转换为整数,实际上您可以转换为以下任何一种:SMALLINT、INTEGER、BIGINT、DECIMAL、DECFLOAT、REAL、DOUBLE、CHAR、VARCHAR(取决于数据,例如 smallint 的大小是有限制的)。
但是为什么要将 table 列更改为整数?为什么不用 DECIMAL 呢?或者,不要改变您的 tables 以适应 BI 产品,而是使用转换为十进制的视图。您还可以通过视图控制舍入。
虽然今天的 BI 工具可能会遇到这种数据类型的问题,但该数据类型是基于标准 EEE754r 的,在未来版本的 BI 工具中它可能不会成为问题。也许在更改任何 table 之前阅读 DECFLOAT: The data type of the future。
我同意 @Used_by_Already 的观点,您最好更新您的 BI 工具,以便它们与 DECFLOAT
一起使用,或者使用 VIEW
s 转换为 INT
或DECIMAL
(甚至FLOAT
!?)在飞行中。
但是如果您确实想要更改现有 table 的数据类型,您可以这样做(有一些限制),如下例所示
create table df(df decfloat(16) not null primary key) organize by row;
insert into df values 1.0, 2.00, 3.120;
select * from df;
给予
DF
------------------------
1.0
2.00
3.120
3 record(s) selected.
然后
alter table df alter column df set data type integer;
returns
SQL0478N The statement failed because one or more dependencies exist on the
target object. Target object type: "COLUMN". Name of an object that is
dependent on the target object: "SQL180924130131030". Type of object that is
dependent on the target object: "PRIMARY KEY". SQLSTATE=42893
所以
alter table df drop primary key;
alter table df alter column df set data type integer;
然后
select * from df
给予
DF
-----------
1
2
3
3 record(s) selected.
但是
insert into df values 4
由于重组挂起而失败
SQL0668N Operation not allowed for reason code "7" on table "PAUL.DF".
SQLSTATE=57016
所以
call admin_cmd('reorg table df');
insert into df values 4;
alter table df add primary key (df);
select * from df;
给予
DF
-----------
1
2
3
4
4 record(s) selected.