简体   繁体   中英

Error converting data type varchar to numeric in sql server

I have created a stored procedure for Save InvoicePayment. When I tried to execute the stored procedure, I get the below error:

Msg 8114, Level 16, State 5, Procedure USP_SaveInvoicePayment_LKO, Line 0 [Batch Start Line 118] Error converting data type varchar to numeric.

ALTER PROC Usp_saveinvoicepayment_lko @RECEIPTNO       VARCHAR(500), 
                                  @INVOICEID       BIGINT, 
                                  @PayableAmount   NUMERIC(10, 2), 
                                  @RECEIPTDT       DATE, 
                                  @PAYMENTMODE     VARCHAR(50), 
                                  @TRANREF         VARCHAR(500), 
                                  @USERID          BIGINT, 
                                  @CHEQUEDATE      DATE, 
                                  @isValid         INT, 
                                  @remark          VARCHAR(100), 
                                  @InvoiceNo       VARCHAR(50), 
                                  @PaymentRecieved VARCHAR(20), 
                                  @PreviousBalance NUMERIC(10, 2), 
                                  @ChequeNumber    VARCHAR(20), 
                                  @PaymentMonth    VARCHAR(20), 
                                  @PaymentDate     DATE, 
                                  @CollectorMobile INT, 
                                  @Latitude        NUMERIC(10, 2), 
                                  @Longitude       NUMERIC(10, 2), 
                                  @RESPONSECODE    INT output, 
                                  @RESPONSEMESSAGE VARCHAR(255) output, 
                                  @IDRESPONSE      VARCHAR(200) output 
AS 
SET nocount ON 

 BEGIN 
  --declare @PRJCD varchar(10),             
  --@KML GEOMETRY   ,       
  --@rowcount int          
  BEGIN try 
      --SELECT @PRJCD=PRJCD FROM PRJMST WHERE ID=@PRJID           
      IF @RECEIPTNO IS NULL 
        BEGIN 
            DECLARE @id_out TABLE 
              ( 
                 id VARCHAR(200) 
              ) 

            --set @ENTRYDATE=getdate()       
            -- select 





convert(varchar,isnull(max(convert(numeric,substring(receiptno,21,50)))+1,1)) 
   from invoicepayment where invoiceid=@invoiceid      
                INSERT INTO invoicepayment_lko 
                            (receiptno, 
                             invoiceid, 
                             payableamount, 
                             receiptdt, 
                             paymentmode, 
                             trnreference, 
                             userid, 
                             entrydate, 
                             chequedate, 
                             isvalid, 
                             remark, 
                             invoiceno, 
                             paymentrecieved, 
                             previousbalance, 
                             chequenumber, 
                             paymentmonth, 
                             paymentdate, 
                             collectormobile, 
                             latitude, 
                             longitude) 
                output      inserted.receiptno 
                INTO @id_out 
                VALUES      ( @INVOICENO + '/REC/' 
                              + (SELECT CONVERT(VARCHAR, Isnull( 
                                        Max(CONVERT(NUMERIC, Substring( 
                                        receiptno, 21, 
                                        50 
                                          ))) 
                                        + 1, 1)) 
                                 FROM   invoicepayment_lko 
                                 WHERE  invoiceid = @invoiceid), 
                              @INVOICEID, 
                              @PayableAmount, 
                              @RECEIPTDT, 
                              @PAYMENTMODE, 
                              @TRANREF, 
                              @USERID, 
                              Getdate(), 
                              @CHEQUEDATE, 
                              @isValid, 
                              @remark, 
                              @InvoiceNo, 
                              @PaymentRecieved, 
                              @PreviousBalance, 
                              @ChequeNumber, 
                              @PaymentMonth, 
                              @PaymentDate, 
                              @CollectorMobile, 
                              @Latitude, 
                              @Longitude) 

                SELECT @IDRESPONSE = id 
                FROM   @id_out 

                SELECT Ident_current(id) 
                FROM   invoicepayment_lko 

                UPDATE invoicedetails 
                SET    balanceamt = balanceamt - @PayableAmount 
                WHERE  id = @INVOICEID 
            END 
          ELSE 
            BEGIN 
                UPDATE invoicepayment_lko 
                SET    chequedate = @CHEQUEDATE, 
                       invoiceid = @INVOICEID, 
                       payableamount = @PayableAmount, 
                       receiptdt = @RECEIPTDT, 
                       paymentmode = @PAYMENTMODE, 
                       trnreference = @TRANREF, 
                       userid = @USERID, 
                       updatedt = Getdate(), 
                       isvalid = @isValid, 
                       remark = @remark, 
                       invoiceno = @InvoiceNo, 
                       paymentrecieved = @PaymentRecieved, 
                       previousbalance = @PreviousBalance, 
                       chequenumber = @ChequeNumber, 
                       paymentmonth = @PaymentMonth, 
                       paymentdate = @PaymentDate, 
                       collectormobile = @CollectorMobile, 
                       latitude = @Latitude, 
                       longitude = @Longitude 
                WHERE  receiptno = @RECEIPTNO 

                SET @IDRESPONSE = @RECEIPTNO 
            END 

          DECLARE @totalPayment NUMERIC(10, 2) 

          SELECT @totalPayment = Sum(payableamount) 
          FROM   invoicepayment_lko 
          WHERE  invoiceid = @invoiceid 
                 AND isvalid = 1 

          SELECT @totalPayment, 
                 @invoiceid 
      END try 

      BEGIN catch 
          SELECT @RESPONSECODE = Error_number(), 
                 @RESPONSEMESSAGE = Error_message(); 

          --set @RESPONSEMESSAGE= @prjid;         
          SELECT @RESPONSECODE, 
                 @RESPONSEMESSAGE 
      END catch 

      IF @RESPONSECODE IS NULL 
        BEGIN 
            IF @isValid = 0 
              BEGIN 
                  SET @RESPONSEMESSAGE = 'Payment marked as Invalid' 
              END 
            ELSE 
              BEGIN 
                  SET @RESPONSEMESSAGE = 'Payment created/Updated 
      Successfully' 
              END 

            SET @RESPONSECODE = 200 
        END 
      END 

    --Execution 
    DECLARE @RESPONSECODE    INT = 20, 
            @RESPONSEMESSAGE VARCHAR(255) = 'Payment Created', 
            @IDRESPONSE      VARCHAR(200) = 'No Response' 

     EXEC Usp_saveinvoicepayment_lko 
      NULL, 
      123, 
      '87.09', 
      '2019-07-11', 
      'Debit', 
      'test', 
      12, 
      '2019-08-11', 
      1, 
      'test', 
      'LP/0819/0000183', 
      'yES', 
      '90.09', 
      '9875', 
      '7', 
      '2019-07-18', 
      988893739, 
      '28.09', 
      '76.09', 
      @RESPONSECODE out, 
      @RESPONSEMESSAGE out, 
      @IDRESPONSE out

Please check your input again. As per the parameters passed in procedure

    EXEC Usp_saveinvoicepayment_lko 
      NULL, 
      123, 
      '87.09', 
      '2019-07-11', 
      'Debit', 
      'test', 
      12, 
      '2019-08-11', 
      1, 
      'test', 
      'LP/0819/0000183', 
      'yES', 
      '90.09', 
      '9875', 
      '7', 
      '2019-07-18', 
      988893739,       --- this is one of the reason for your error as you took this as int, but this value will definitly not convert into int.
      '28.09', 
      '76.09', 
      @RESPONSECODE out, 
      @RESPONSEMESSAGE out, 
      @IDRESPONSE out

If you can see for phone number is taken as int and int is not right data type for your field mobile number, either take BigInt or use varchar

My suggestion for mobile number field.

   @CollectorMobile BIGINT  or VARCHAR(15)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM