OK, below is the code. Here are the impacted tables and what I am trying to do.
TblPO is the Purchase Order header file and has associated with it the corresponding line items in TblPoDetails. When an item is received a TblPOReceipts record is created and the QtyOnHand and QtyOnOrder fields in Products are increased and decreased by the QtyReceived.
Both TblPO and TblPoDetails have a ReceivedComplete and ReceivedPartial flag defined. If a received qty does not match the detail qty ordered, the ReceivedPartial flags are set for both tables.
A detail record is flagged complete when the accumulated received quantities meet or exceed the ordered quantity. A header record is flagged complete when all corresponding detail records are flagged complete.
I am trying to put it in a transaction so that all updates would have to be successful. I read somewhere that SQL Server checks the master table for sp_ so I used spc to save the lookup. Also, it sticks in my mind that you should always use dbo for things of this nature. Thanks for your willingness to help by looking at this.
************************* Code Begin *********************
/* Ensure we are using correct database */
USE Database1
GO
IF DB_NAME() <> 'Database1' BEGIN
RAISERROR('Wrong database',16,10)
RETURN
END
/* Delete the Stored Procedure if it exists */
IF OBJECT_ID('dbo.spcProcessTblPOReceipt') IS NOT NULL
DROP PROCEDURE dbo.spcProcessTblPOReceipt
GO
/* Stored Procedure for POReceipts */
CREATE PROCEDURE dbo.spcProcessTblPOReceipt
-- Input parms
@PO_ID int,
@PODet_ID int,
@ProductID int,
@NewQty float, -- Quantity received via Receiving form user entry
@DetailsQty float -- Quantity ordered from TblPoDetails record
AS
/* All changes to TblPOReceipts, TblPoDetails and TblPO must be valid */
BEGIN TRANSACTION
DECLARE @Err int,
@DetailsCount int,
@DetailsComplete int,
@TotalReceiptsQty float
/* Create a new record in TblPOReceipts */
INSERT INTO dbo.TblPOReceipts (PO_ID, PODet_ID, Quantity)
VALUES (@PO_ID, @PODet_ID, @NewQty)
SET @Err = @@ERROR
IF @Err <> 0 BEGIN
ROLLBACK TRANSACTION
RETURN(@Err)
END
/* Update quantity fields in Products table */
UPDATE dbo.Products
-- SET DateUpdated = getdate(), -- Per owner, system changes should NOT update
SET Qty_On_Hand = Qty_On_Hand + @NewQty,
Qty_On_Order = Qty_On_Order - @NewQty
WHERE Products.ProductID = @ProductID
SET @Err = @@ERROR
IF @Err <> 0 BEGIN
ROLLBACK TRANSACTION
RETURN(@Err)
END
/* Could be 1 record if received complete or multiple partial records */
SET @TotalReceiptsQty = (SELECT SUM(Quantity) FROM dbo.TblPOReceipts
WHERE dbo.TblPOReceipts.PODet_ID = @PODet_ID)
SET @Err = @@ERROR
IF @Err <> 0 BEGIN
ROLLBACK TRANSACTION
RETURN(@Err)
END
/* Flag complete if details order qty has been met or exceeded */
IF @TotalReceiptsQty >= @DetailsQty BEGIN
/* Flag details record as complete */
UPDATE dbo.TblPoDetails
SET ReceivedComplete = 1
WHERE dbo.TblPoDetails.PODet_ID = @PODet_ID
SET @Err = @@ERROR
IF @Err <> 0 BEGIN
ROLLBACK TRANSACTION
RETURN(@Err)
END
/* Count number of detail records for this PO */
SET @DetailsCount = (SELECT Count(*) FROM dbo.TblPoDetails
WHERE dbo.TblPoDetails.PO_ID = @PO_ID)
SET @Err = @@ERROR
IF @Err <> 0 BEGIN
ROLLBACK TRANSACTION
RETURN(@Err)
END
/* Count number of completed detail records for this PO */
SET @DetailsComplete = (SELECT Count(*) FROM dbo.TblPoDetails PD
WHERE PD.PO_ID = @PO_ID AND PD.ReceivedComplete = 1)
SET @Err = @@ERROR
IF @Err <> 0 BEGIN
ROLLBACK TRANSACTION
RETURN(@Err)
END
/* If all details records are complete then this PO is complete */
IF @DetailsCount = @DetailsComplete BEGIN
UPDATE dbo.TblPO
SET ReceivedComplete = 1
WHERE dbo.TblPO.PO_ID = @PO_ID
END
SET @Err = @@ERROR
IF @Err <> 0 BEGIN
ROLLBACK TRANSACTION
RETURN(@Err)
END
END
ELSE BEGIN -- New receipts qty is less than details ordered qty
/* Update TblPoDetails ReceivedPartial flag */
UPDATE dbo.TblPoDetails
SET ReceivedPartial = 1
WHERE dbo.TblPoDetails.PODet_ID = @PODet_ID
SET @Err = @@ERROR
IF @Err <> 0 BEGIN
ROLLBACK TRANSACTION
RETURN(@Err)
END
/* Update TblPO ReceivedPartial flag */
UPDATE dbo.TblPO
SET ReceivedPartial = 1
WHERE dbo.TblPO.PO_ID = @PO_ID
SET @Err = @@ERROR
IF @Err <> 0 BEGIN
ROLLBACK TRANSACTION
RETURN(@Err)
END
END
/* Everything worked OK */
COMMIT TRANSACTION
GO
************************ Code End ************************