1
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

Sample 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>'

Error:

Msg 8115, Level 16, State 8, Procedure Update_MCR, Line 42
Arithmetic overflow error converting decimal to data type numeric.

I am not able to find out the why this error occur and for which value. In case of insert I take another way as mention in this site. But again a face the same problem.

2
  • 1
    First of all: if you have XML that you pass into this procedure, why is the datatype of the @xmlString not XML?? Makes no sense at all - especially since ntext is deprecated and should not be used anymore. Also: don't use FLOAT! It's not precise - use DECIMAL(15,2) or something like that instead Commented Dec 20, 2012 at 10:44
  • Please supply the definition of Monthly_Salary_Statement table. Commented Dec 20, 2012 at 10:46

1 Answer 1

2

I would:

  • change the datatype for @xmlString to XML - it's XML, after all - right?
  • get rid of the cursor ! You don't need it - it's just a performance killer - drop it
  • get rid of the "old-style" OPENXML stuff - use the native XQuery support of SQL Server! Much easier to use

So my procedure would be something like:

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
Sign up to request clarification or add additional context in comments.

1 Comment

After changing float to decimal the same error is giving. I can not find out why this problem occur. This problem occur in case of float,decimal,numeric data type.other data types works fine. the solution provided is working. Although i want to find out what is going wrong with that procedure. can you tell me where the actual problem is ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.