发票创建的存储过程

时间:2019-07-24 06:09:58

标签: sql sql-server stored-procedures sql-query-store

我已经为新发票创建创建了存储过程。执行存储过程时,无法在表中看到任何数据。你能帮我吗?

我在这里提到Table模式和结构。

表- InvoiceDetails (this contain the basic invoice info)

我创建的存储过程:-

ALTER PROC Demoinvoice @Id                 INT, 
                   @InvoiceNumber      NVARCHAR(150), 
                   @PRJID              BIGINT, 
                   @CustomerId         NVARCHAR(100), 
                   @InvoiceDate        DATE, 
                   @InvoiceMonth       VARCHAR(20), 
                   @DUEDATE            DATE, 
                   @UCCCATID           BIGINT, 
                   @rate               NUMERIC(10, 2), 
                   @Quantity           NUMERIC(10, 2), 
                   @ActualAmount       NUMERIC(10, 2), 
                   @PendingAmount      NUMERIC(10, 2), 
                   @FromDate           DATE, 
                   @ToDate             DATE, 
                   @USERID             BIGINT, 
                   @deleteflag         INT, 
                   @RESPONSECODE       INT output, 
                   @RESPONSEMESSAGE    VARCHAR(255) output, 
                   @INVOICENO_RESPONSE VARCHAR(20) output 
AS 
SET nocount ON 

BEGIN -- Main 
  DECLARE @PRJCD NCHAR, 
          @aa    NVARCHAR(50), 
          @aa2   NVARCHAR(50) 

  BEGIN try 
      SELECT @PRJCD = prjcd 
      FROM   prjmst 
      WHERE  id = @PRJID 

      SET @aa=@prjcd + 'P/' 
              + CONVERT(VARCHAR, Year(Getdate())-2000) 
              + CONVERT(VARCHAR, Year(Getdate())-2000+1) 
      SET @aa2=@prjcd + 'K/' 
               + CONVERT(VARCHAR, Year(Getdate())-2000) 
               + CONVERT(VARCHAR, Year(Getdate())-2000+1) 

      IF @InvoiceNumber IS NULL 
        BEGIN -- if block 
            DECLARE @id_out TABLE 
              ( 
                 id BIGINT 
              ) 

            --set @aa=@prjcd+'K/'+ RIGHT('0' + RTRIM(MONTH(@invoicedt)), 
2)+RIGHT('0' + RTRIM(year(@invoicedt)), 2)   
            IF EXISTS(SELECT * 
                      FROM   vw_invoicedetailslko 
                      WHERE  ( ( CONVERT(DATE, @FromDate) BETWEEN 
                                 fromdt AND todt 
                               ) 
                                OR ( CONVERT(DATE, @ToDate) BETWEEN 
                                     fromdt AND todt ) 
                             ) 
                             AND refid = @CustomerId 
                             AND statusid <> 9) 
              BEGIN 
                  SET @RESPONSEMESSAGE = 
'Can not create invoice for selected date range ! another Invoice is already 
 created for selected period'
SET @RESPONSECODE = 400 
END 
ELSE 
BEGIN -- else block 
  SELECT @InvoiceNumber = @PRJCD + 'K/' 
                          + RIGHT('0' + Rtrim(Month(@InvoiceDate)), 2) 
                          + RIGHT('0' + Rtrim(Year(@InvoiceDate)), 2) 
                          + '/' 
                          + Replicate('0', 5-Len(Isnull(Max(CONVERT(NUMERIC, 
                          Substring( 
                                 invoicenumber, 9, 20))), 0)+1)) 
                          + CONVERT(VARCHAR, Isnull(Max(CONVERT(NUMERIC, 
                          Substring 
                          ( 
                                 invoicenumber, 9, 20))), 0)+1 ) 
  FROM   invoicedetails 
  WHERE  invoicenumber LIKE @aa + '%' 

  SELECT @InvoiceNumber = @aa + '/' 
                          + Replicate('0', 7-Len(Isnull(Max(CONVERT(NUMERIC, 
                          Substring( 
                                 pinvoiceno, 9, 20))), 0)+1)) 
                          + CONVERT(VARCHAR, Isnull(Max(CONVERT(NUMERIC, 
                          Substring 
                          ( 
                                 pinvoiceno, 9, 20))), 0)+1 ) 
  FROM   invoice 
  WHERE  pinvoiceno LIKE @aa + '%' 

  INSERT INTO invoicedetails 
              (invoicenumber, 
               invoicedate, 
               fromdate, 
               todate, 
               duedate, 
               pendingamount, 
               customerid, 
               rate, 
               quantity, 
               actualamount, 
               invoicemonth, 
               prjid, 
               ucccatid, 
               userid) 
  output      inserted.id 
  INTO @id_out 
  VALUES      (@InvoiceNumber, 
               @InvoiceDate, 
               @FromDate, 
               @ToDate, 
               @DUEDATE, 
               @PendingAmount, 
               @CustomerId, 
               @Rate, 
               @Quantity, 
               @ActualAmount, 
               @InvoiceMonth, 
               @PRJID, 
               @UCCCATID, 
               @USERID) 

  SET @RESPONSEMESSAGE = 'Invoice created Successfully' 
  SET @RESPONSECODE = 200 
  END -- else block 
 END -- if block 
ELSE 
BEGIN 
  UPDATE invoicedetails 
  SET    invoicenumber = @InvoiceNumber, 
         invoicedate = @InvoiceDate, 
         fromdate = @FromDate, 
         todate = @ToDate, 
         duedate = @DueDate, 
         pendingamount = @PendingAmount, 
         customerid = @CustomerId, 
         rate = @Rate, 
         quantity = @Quantity, 
         actualamount = @ActualAmount, 
         invoicemonth = @InvoiceMonth, 
         prjid = @PRJID, 
         ucccatid = @UCCCATID, 
         userid = @USERID 
  WHERE  invoicenumber = @InvoiceNumber 

  SET @RESPONSEMESSAGE = 'Invoice updated Successfully' 
  SET @RESPONSECODE = 200 
  END 
  END try 

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

-- Execution 
DECLARE @RESPONSECODE       INT = 20, 
        @RESPONSEMESSAGE    VARCHAR(255) = 'success', 
        @INVOICENO_RESPONSE VARCHAR(20) = 'No response' 

EXEC Demoinvoice 
  1, 
  '00001', 
  1, 
  '1010', 
  '2019-06-09', 
  '0', 
  '2019-05-10', 
  123, 
  23.09, 
  7, 
  878, 
  15365, 
  '2019-07-11', 
  '2019-07-21', 
  12, 
  1, 
  @RESPONSECODE out, 
  @RESPONSEMESSAGE out, 
  @INVOICENO_RESPONSE out 

 Please let me know where I am going wrong.

0 个答案:

没有答案