SQL 8115将数值转换为数据类型numeric的算术溢出错误

时间:2012-08-23 18:25:33

标签: sql sql-server-2005

我在SQL Server 2005上获得了一个错误

Server: Msg 8115, 
Arithmetic overflow error converting numeric to data type numeric.

当我尝试插入值= X (DECIMAL(3,2))* Y (INT)
MYCOLUMN列中键入DECIMAL (10,3)

如何解决此问题?

更新

下面有一些代码可以了解情况

-- ============================================================
--   Table : GP_WGENTH
-- ============================================================
CREATE TABLE #GP_WGENTH
(
     KEYINTERNAL                     CHAR(32)              NOT NULL,
     KEYHOUR                         INT                   NOT NULL,
     FIRM                            CHAR(5)               NOT NULL,
     CENTRE                          CHAR(2)               NOT NULL,
     SERVICE                         CHAR(6)               NOT NULL,
     AGENT                           CHAR(5)               NOT NULL,
     VERSION                         VARCHAR(3)            NOT NULL,
     DATE_ROULEMENT                  DATETIME              NOT NULL,
     SERVICEINSTANCE                 SMALLINT              NOT NULL,
     TYPEHOUR                        CHAR(9)               NOT NULL,
     QUANTITEFIXE                    DECIMAL(12,2)         NULL    ,
     TYPECMP                         INT                   NULL    ,
     PERCENTCALC                     DECIMAL(3,2)          NULL    
)
-- ============================================================
--   Table : GP_WGENSVCTH
-- ============================================================
CREATE TABLE #GP_WGENSVCTH
(
    KEYINTERNAL                     CHAR(32)              NOT NULL,
    KEYHOUR                         INT                   NOT NULL,
    FIRM                            CHAR(5)               NOT NULL,
    CENTRE                          CHAR(2)               NOT NULL,
    SERVICE                         CHAR(6)               NOT NULL,
    AGENT                           CHAR(5)               NOT NULL,
    VERSION                         VARCHAR(3)            NOT NULL,
    DATE_ROULEMENT                  DATETIME              NOT NULL,
    SERVICEINSTANCE                 SMALLINT              NOT NULL,
    TYPEHOUR                        CHAR(9)               NOT NULL,
    VALUE                           DECIMAL(10,3)         NOT NULL
)
GO

INSERT INTO #GP_WGENSVCTH 
            (KEYINTERNAL, 
             KEYHOUR, 
             FIRM, 
             CENTRE, 
             SERVICE, 
             AGENT, 
             VERSION, 
             DATE_ROULEMENT, 
             SERVICEINSTANCE, 
             TYPEHOUR, 
             VALUE) 
SELECT WTH.KEYINTERNAL, 
       WTH.KEYHOUR, 
       WTH.FIRM, 
       WTH.CENTRE, 
       WTH.SERVICE, 
       WTH.AGENT, 
       WTH.VERSION, 
       WTH.DATE_ROULEMENT, 
       WTH.SERVICEINSTANCE, 
       WTH.TYPEHOUR, 
       WTH.PERCENTCALC * SUM(SC.LENGTH) -- !!! BOOOM !!!!!!!!! HERE
FROM   #GP_WGENTH WTH 
       INNER JOIN GP_SERVICE_HABILLAGE_COMPONENT SC 
               ON WTH.FIRM = SC.FIRM 
                  AND WTH.CENTRE = SC.CENTRE 
                  AND WTH.SERVICE = SC.SERVICE 
                  AND WTH.VERSION = SC.VERSION 
                  AND WTH.TYPECMP = SC.TYPECMP 
WHERE  WTH.KEYINTERNAL = 'august 23 2012  10:45:21:027PM     ' 
       AND SC.LENGTH IS NOT NULL 
GROUP  BY WTH.KEYINTERNAL, 
          WTH.KEYHOUR, 
          WTH.FIRM, 
          WTH.CENTRE, 
          WTH.SERVICE, 
          WTH.AGENT, 
          WTH.VERSION, 
          WTH.DATE_ROULEMENT, 
          WTH.SERVICEINSTANCE, 
          WTH.TYPEHOUR, 
          WTH.PERCENTCALC 

3 个答案:

答案 0 :(得分:6)

必要的比例,精度和长度的规则是well covered in MSDN documentation

十进制只是一个缩放的整数,所以让我们看一个没有小数点的例子。

例如X DECIMAL(3, 0)Y DECIMAL(3, 0)其中X = 100Y = 100X * Y = 10000不适合DECIMAL(3, 0),需要{{1} }}。事实上,DECIMAL(5, 0)需要999 * 999 = 998001

因此,尝试插入乘以两个十进制列的结果只适用于目标列的容量至少与MSDN表中针对不同操作的公式所指示的容量一样大。

答案 1 :(得分:5)

这里有两个选项:

一种是扩展表VALUE#GP_WGENSVCTH字段的大小。显然,decimal(10,3)不足以容纳您尝试插入的值。

两种方法是将表#GP_WGENSVCTH中列的数据类型更改为float

VALUE float

更改为浮点数,您不必担心该值对于为小数设置的精度和比例而言太大

declare @dec decimal(10, 3) = 1111111.111 
declare @int int = 3

declare @float float

set @float = @dec * @int

select @float

请参阅上述代码的SQL Fiddle with demo

答案 2 :(得分:2)

假设您的评论中指出的X和Y的最大乘积可以是1 111 111 111,111,只需将MyColumn更改为decimal(13, 3)

试一试:

DECLARE @x decimal(13, 3)
SET @x = 1111111111.111 

没有错误。

您无法将1 111 111 111,111存储在decimal(10, 3)