Chapter 10A – Managing Databases with SQL Server 2017
Page 10A-133
IF @NumberOfSaleItems = 1
— Last SALE_ITEM, deny deletion of SALE_ITEM.
BEGIN
— Print the error message.
PRINT ‘********************************************************************’
PRINT ”
PRINT ‘ You have attempted to delete the last sale item in a sale. ‘
PRINT ”
PRINT ‘ Sale Sale Number = ‘ +(CONVERT(Char(10),@SaleID))
— Rollback the transaction and return.
ROLLBACK TRANSACTION;
RETURN;
END;
ELSE
— Deletion of SALE_ITEM.
BEGIN
DELETE FROM SALE_ITEM
WHERE SaleID = @SaleID
AND SaleItemID = @SaleItemID;
END;
END;
To test this, run the following commands:
INSERT INTO SALE (CustomerID, EmployeeID, SaleDate, Subtotal, Tax, Total)
VALUES (
1, 1, ’15-Oct–18′, 850.00, 70.55, 920.55);