Chapter 10A – Managing Databases with SQL Server 2017
Page 10A-173
Figure 7-29 contains the logic to delete all SHIPMENT_ITEMs and the SHIPMENT. Here is the
trigger code:
CREATE OR ALTER TRIGGER [Allow_Deletion_Of_SHIPMENT] ON
[dbo].[DeleteShipmentItemShipmentView]
INSTEAD OF DELETE
BEGIN
— Determine if this is the last SHIPMENT_ITEM in the SHIPMENT.
SELECT @ShipmentNumber = D.ShipmentID, @PurchaseNumber = D.PurchaseItemID
FROM DELETED AS D;
SELECT @NumberOfShipmentItems = COUNT(*)
FROM SHIPMENT_ITEM
WHERE ShipmentID = @ShipmentNumber;
PRINT ‘********************************************************************’
PRINT ”
PRINT ‘ You have deleted the last shipment item in a shipment’
PRINT ‘ and therefore also deleted the associated shipment.’
PRINT ”
ELSE
— Deletion of SHIPMENT_ITEM.
BEGIN
DELETE FROM SHIPMENT_ITEM
WHERE ShipmentID = @ShipmentNumber
AND PurchaseItemID = @PurchaseNumber;
PRINT ‘********************************************************************’
PRINT ”
PRINT ‘ You have deleted the following shipment item:’