如何使用DB2 SQL将DECFLOAT更改为INT?

时间:2018-09-22 00:40:55

标签: database db2 db2-luw

简而言之,我试图摆脱数据库中所有这些DECFLOAT类型,因为许多报告工具(例如Tableau)都不想使用它们。我是DB2的新手,并且已经找到了很多在线资料,这些资料可以从任何数据类型-> DECFLOAT中获取,但是从DECFLOAT-> INT中获取并没有什么实质意义。

这甚至可能吗?有什么建议吗?

2 个答案:

答案 0 :(得分:3)

根据Table 2. Supported casts between built-in data types支持将DECFLOAT转换为整数,实际上您可以实现以下任意一种转换:SMALLINT,INTEGER,BIGINT,DECIMAL,DECFLOAT,REAL,DOUBLE,CHAR,VARCHAR(取决于数据) ,例如,smallint的大小有限制)。

但是为什么要将表列更改为整数?为什么不选择DECIMAL?或者,请勿更改表以适合BI产品,而应使用强制转换为十进制的视图。您还可以控制视图的四舍五入。

尽管当今的BI工具可能对此数据类型存在问题,但是该数据类型基于标准EEE754r,在BI工具的未来版本中,可能会不再那么麻烦。也许在更改任何表格之前先阅读DECFLOAT: The data type of the future

答案 1 :(得分:0)

我同意@Used_by_Already,您最好是更新BI工具,使其与DECFLOAT一起使用,或者使用VIEW s转换为INTDECIMAL (甚至是FLOAT!?)。

但是,如果您确实想更改现有表的数据类型,则可以这样做(有一些限制),如下面的示例所示

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;

返回

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.