RODBC - 值不适合数字

时间:2015-01-26 23:20:20

标签: sql r odbc netezza

您好,并提前致谢。我使用RODBC与Netezza数据库建立ODBC连接,我正在尝试将数据帧写入数据库表。我一直收到以下错误:

HY000 46 ERROR:  Numeric overflow.  Value does not fit in a numeric(10,10)

我不明白我是如何得到它的,因为我的所有数值都四舍五入到最接近的数字!我有我的SQL查询,我在下面创建了表和我的R代码,并提供了我正在尝试写入数据库的示例:

SQL代码

                CREATE TABLE R_FORECAST
                (
                          ID INTEGER NOT NULL,
                          NUMBER CHARACTER(7) NOT NULL,
                          ITEM_NO INTEGER NOT NULL,
                          ITEM_DESC VARCHAR(50),
                          FORECASTDATE DATE NOT NULL,
                          MEANFORECAST NUMERIC(10,10),
                          UPPER50 NUMERIC(10,10),
                          UPPER75 NUMERIC(10,10),
                          UPPER90 NUMERIC(10,10),
                          LOWER50 NUMERIC(10,10),
                          LOWER75 NUMERIC(10,10),
                          LOWER90 NUMERIC(10,10),
                          CREATED_DT TIMESTAMP DEFAULT "TIMESTAMP"(now(0)::"VARCHAR"),
                          TRANSACTION_DT DATE NOT NULL
                )
                DISTRIBUTE ON (ID);

R代码

    loadprod <- odbcConnect(dsn="dsn", uid = "uid", pwd = "pwd",case="nochange", colQuote=c() )

    head(FORECAST)

        TRANSACTION_DT  ID  NUMBER  ITEM_NO ITEM_DESC   FORECASTDATE    MEANFORECAST    UPPER50 UPPER75 UPPER90 LOWER50 LOWER75 LOWER90 CREATED_DT
        1/26/2015   4192    2239    9664    DESC    2/16/2015   11  13  17  22  9   7   2   1/26/2015 16:38
        1/26/2015   4192    2239    9664    DESC    2/17/2015   14  16  19  24  11  10  7   1/26/2015 16:38
        1/26/2015   4192    2239    9664    DESC    2/18/2015   13  15  18  21  10  9   6   1/26/2015 16:38
        1/26/2015   4192    2239    9664    DESC    2/19/2015   13  15  18  24  10  8   6   1/26/2015 16:38
        1/26/2015   4192    2239    9664    DESC    2/20/2015   15  18  21  26  13  11  9   1/26/2015 16:38
        1/26/2015   4192    2239    9664    DESC    2/21/2015   13  16  19  24  11  9   7   1/26/2015 16:38

sqlSave(loadprod, FORECAST, tablename = "R_FORECAST", append = T,
            rownames = F, colnames = FALSE, verbose = T,
            safer = TRUE, addPK = FALSE, 
            varTypes <- tmp$TYPE_NAME,
            fast = TRUE, test = FALSE, nastring = NULL)

1 个答案:

答案 0 :(得分:1)

NUMERIC(10,10)只能保存小于1的值.NUMERIC类型定义中的第一个值是精度,即存储的总位数,第二个值是刻度,这是如何许多这些数字都在小数点之后。

TESTDB.ADMIN(ADMIN)=> create table numeric_test (col1 numeric(10,10), col2 numeric(10,0));
CREATE TABLE
TESTDB.ADMIN(ADMIN)=> insert into numeric_test values (0.1234567890, 1234567890);
INSERT 0 1
TESTDB.ADMIN(ADMIN)=> select * from numeric_test;
     COL1     |    COL2
--------------+------------
 0.1234567890 | 1234567890
(1 row)

如果您尝试将需要小数点左侧数字的任何内容插入到COL1中,您将收到正在经历的错误。

TESTDB.ADMIN(ADMIN)=> insert into numeric_test values (10, 11);
ERROR:  Numeric overflow.  Value does not fit in a numeric(10,10)

如果您要查找的是存储整数值,通常应该使用INTEGER而不是NUMERIC。这提供了许多好处,包括通常更好的存储效率和对区域地图的支持。