算术溢出错误将十进制转换为数据类型数字

时间:2012-12-20 10:22:36

标签: sql-server stored-procedures

ALTER PROCEDURE [dbo].[Update_MCR]
     @xmlString  ntext
    ,@Message nvarchar(500) output
AS
BEGIN

    SET NOCOUNT ON;
    declare  @SL int,@Basic  float, @Grad_pay  float, @DA  float, @HRA  float, @MA  float, @Ptax  float, @Itax  float, @pf  float, @LIC  float, @Month_Of  datetime
    Declare @intDoc1 as int
    BEGIN TRANSACTION 
    print @xmlString
    exec sp_xml_preparedocument @intDoc1 OUTPUT, @xmlString
    declare Generate_Rq CURSOR FOR
    SELECT SL,Basic, Grad_pay, DA, HRA, MA, Ptax, Itax, pf, LIC,  Month_Of
    FROM OPENXML (@intDoc1,'/Salary/TransactionSalary',1)
    WITH ( SL int,Basic  float, Grad_pay  float, DA  float, HRA  float, MA  float, Ptax  float, Itax  float, pf  float, LIC  float, Month_Of  datetime)

    OPEN Generate_Rq

    FETCH next FROM Generate_Rq
    INTO @SL,@Basic, @Grad_pay, @DA, @HRA, @MA, @Ptax, @Itax, @pf, @LIC,  @Month_Of

    WHILE @@Fetch_Status<>-1
        BEGIN
            Print 'Line  ' +@Basic+ '    '+ @Grad_pay+ '    '+ @DA+ '    '+ @HRA+ '    '+ @MA+ '    '+ @Ptax+ '    '+ @Itax+ '    '+ @pf+ '    '+ @Month_Of+ '    '+ @LIC+ '    '+ @SL

            UPDATE  [Monthly_Salary_Statement]  
            Set [Basic]=@Basic, [Grad_pay]=@Grad_pay, [DA]=@DA, [HRA]=@HRA, [MA]=@MA, [Ptax]=@Ptax, [Itax]=@Itax, [pf]=@pf,  [LIC]=@LIC
            Where  [SL]=@SL and month([Month_Of])=month(@Month_Of) and year([Month_Of])=year(@Month_Of)

            if(@@ERROR<>0)
                BEGIN
                    rollback transaction
                    Set @Message='sp_Update_Montly_Salary: ' + @@Error 
                    close Generate_Rq
                    deallocate Generate_Rq
                    Return
                END
            FETCH next FROM Generate_Rq
            INTO @SL,@Basic, @Grad_pay, @DA, @HRA, @MA, @Ptax, @Itax, @pf, @LIC,  @Month_Of
        END
    CLOSE Generate_Rq
    DEALLOCATE Generate_Rq
    COMMIT TRANSACTION
    set  @Message='True'
END

示例XML:

<Salary>
  <TransactionSalary SL="8" Basic="12560.00" Grad_pay="4800.00" DA="7812.00" HRA="2604.00" MA="300.00" Ptax="150.00" pf="2000.00" Itax="200.00" LIC="0.00" Month_Of="20-Dec-2012" /> 
  <TransactionSalary SL="7" Basic="9860.00" Grad_pay="4100.00" DA="6282.00" HRA="2094.00" MA="300.00" Ptax="150.00" pf="2000.00" Itax="0.00" LIC="0.00" Month_Of="20-Dec-2012" /> 
  <TransactionSalary SL="9" Basic="11850.00" Grad_pay="4800.00" DA="7493.00" HRA="2498.00" MA="300.00" Ptax="150.00" pf="2000.00" Itax="0.00" LIC="200.00" Month_Of="20-Dec-2012" /> 
  </Salary>'

错误:

  

Msg 8115,Level 16,State 8,Procedure Update_MCR,Line 42
  算术溢出错误将十进制转换为数据类型数字。

我无法找出此错误发生的原因以及值。在插入的情况下,我采取在本网站提到的另一种方式。但再一次面对同样的问题。

1 个答案:

答案 0 :(得分:2)

我会:

  • @xmlString的数据类型更改为XML - 它毕竟是XML - 对吧?
  • 摆脱光标!你不需要它 - 它只是一个性能杀手 - 放弃它
  • 摆脱“旧式”OPENXML的东西 - 使用SQL Server的原生XQuery支持!更容易使用

所以我的程序就像:

ALTER PROCEDURE [dbo].[Update_MCR]
     @xmlString  XML,
     @Message nvarchar(500) output
AS
BEGIN
    SET NOCOUNT ON;

    BEGIN TRY
        BEGIN TRANSACTION 

        ;WITH trsal AS 
        (
            SELECT
                SL = TrSal.value('@SL', 'int'),
                [Basic] = TrSal.value('@Basic', 'decimal(16,2)'),
                [Grad_Pay] = TrSal.value('@Grad_pay', 'decimal(16,2)'),
                [DA] = TrSal.value('@DA', 'decimal(16,2)'),
                [HRA] = TrSal.value('@HRA', 'decimal(16,2)'),
                [MA] = TrSal.value('@MA', 'decimal(16,2)'),
                [Ptax] = TrSal.value('@Ptax', 'decimal(16,2)'),
                [Itax] = TrSal.value('@Itax', 'decimal(16,2)'),
                [pf] = TrSal.value('@pf', 'decimal(16,2)'),
                [LIC] = TrSal.value('@LIC', 'decimal(16,2)'),
                [Month_Of] = TrSal.value('@Month_Of', 'datetime')
             FROM 
                @XmlString.nodes('/Salary/TransactionSalary') AS XTbl(TrSal)    
        )
        UPDATE [Monthly_Salary_Statement]  
        SET 
        [Basic] = trsal.Basic, 
        [Grad_pay] = trsal.Grad_pay, 
        [DA] = trsal.DA, 
        [HRA] = trsal.HRA, 
        [MA] = trsal.MA, 
        [Ptax] = trsal.Ptax, 
        [Itax] = trsal.Itax, 
        [pf] = trsal.pf, 
        [LIC] = trsal.LIC
        FROM
        trsal        
        WHERE
        [SL] = trsal.SL 
        AND MONTH([Month_Of]) = MONTH(trsal.Month_Of) 
        AND YEAR([Month_Of]) = YEAR(trsal.Month_Of)

      COMMIT TRANSACTION

      SET @Message = 'True'

    END TRY
    BEGIN CATCH
       ROLLBACK TRANSACTION

       SET @Message = 'sp_Update_Montly_Salary: ' + ERROR_MESSAGE()
    END
END