5

我在我的 SQL Server 2005 上收到一个错误

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

当我尝试在键入 的列中插入值 = X(DECIMAL(3,2)) * Y(INT)
MYCOLUMNDECIMAL (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 
4

3 回答 3

8

MSDN 文档中很好地介绍了必要的比例、精度和长度的规则。

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

例如X DECIMAL(3, 0)Y DECIMAL(3, 0)whereX = 100Y = 100X * Y = 10000不适合DECIMAL(3, 0),需要DECIMAL(5, 0)。事实上,999 * 999 = 998001这将需要DECIMAL(6, 0).

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

于 2012-08-23T18:51:58.287 回答
5

You have two options here:

One is to expand the size of the VALUE field in the table #GP_WGENSVCTH. Obviously the decimal(10,3) is not large enough to hold the value you are trying to insert.

Two would be to change the datatype of the column in the table #GP_WGENSVCTH to a float

VALUE float

Changing to a float, you will not have to worry about the value being too large for the precision and scale you set for the decimal

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

declare @float float

set @float = @dec * @int

select @float

See SQL Fiddle with demo of the above code

于 2012-08-23T18:56:47.333 回答
2

假设 X 和 Y 的最大乘积可以1 111 111 111,111如您在评论中指出的那样,只需更改MyColumndecimal(13, 3).

试试看:

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

没有错误。

您根本无法存储1 111 111 111,111decimal(10, 3).

于 2012-08-23T18:44:38.133 回答