Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Add Table Valued Parameter to a stored procedure

$
0
0

I have a stored procedure that voids an invoice and puts the items back into inventory and makes available the payment that was used to apply to this invoice... I would like to be able to do all this for a number of invoices at a time using a table valued parameter how would I be able to do it?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:Debra
-- Create date: March 25, 2014
-- Description:Void an invoice.
-- =============================================
CREATE PROCEDURE AR_VOID
-- Add the parameters for the stored procedure here
@Invoice INT,
@InvType nvarchar(3)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- Insert statements for procedure here
IF(@InvType = 'reg')
    BEGIN
UPDATE INV SET ONHAND = ONHAND + ARD.Qty ,STAMPED = CASE WHEN INV.TYPE = 'CIG' THEN STAMPED + ARD.QTY ELSE 0 END, LASTDATE = CONVERT(DATE, GETDATE()) FROM ARD JOIN INV ON ARD.ITEM = INV.ITEM
WHERE ARD.INVOICE = @Invoice

MERGE INTO RECEIPTSH target
    USING (SELECT JOURNAL, SUM(AMOUNT) AMOUNT FROM Applied
    WHERE INVOICE = @Invoice GROUP BY JOURNAL) AS source
    ON target.Journal = source.Journal
    WHEN MATCHED THEN 
    UPDATE 
    SET Applied = Applied - SOURCE.Amount;
    
    DELETE Applied WHERE INVOICE = @Invoice
    END
    ELSE 
    BEGIN
UPDATE INV SET ONHAND = ONHAND - ARD.Qty ,STAMPED = CASE WHEN INV.TYPE = 'CIG' THEN STAMPED - ARD.QTY ELSE 0 END, LASTDATE = CONVERT(DATE, GETDATE()) FROM ARD JOIN INV ON ARD.ITEM = INV.ITEM
WHERE ARD.INVOICE = @Invoice

MERGE INTO ARH target
    USING (SELECT INVOICE, SUM(AMOUNT) AMOUNT FROM CREDITMEMO WHERE CINVOICE = @Invoice GROUP BY INVOICE) AS source
    ON target.INVOICE = source.INVOICE
    WHEN MATCHED THEN 
    UPDATE 
    SET [OPEN] = 'TRUE', CLOSEDATE = NULL, PAID = target.PAID - source.AMOUNT;
    
DELETE CREDITMEMO WHERE CINVOICE = @Invoice
    END

    UPDATE ARH SET SUBTOTAL = 0, TAXES = 0, PAID = 0, STATUS = 'VOD', [OPEN] = 'FALSE', CLOSEDATE = CONVERT(DATE,GETDATE()) WHERE INVOICE = @Invoice
UPDATE ARD SET QTY = 0, ARD.PRICE = 0 WHERE INVOICE = @Invoice
END
GO


Debra has a question


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>