Advertisement
RodrigoPvz

Untitled

Nov 11th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.22 KB | None | 0 0
  1. -- This trigger prevents a row from being inserted in the Purchasing.PurchaseOrderHeader
  2. -- table when the credit rating of the specified vendor is set to 5 (below average).  
  3.  
  4. CREATE TRIGGER Purchasing.LowCredit ON Purchasing.PurchaseOrderHeader  
  5. AFTER INSERT  
  6. AS  
  7. IF EXISTS (SELECT *  
  8.            FROM Purchasing.PurchaseOrderHeader AS p  
  9.            JOIN inserted AS i  
  10.            ON p.PurchaseOrderID = i.PurchaseOrderID  
  11.            JOIN Purchasing.Vendor AS v  
  12.            ON v.BusinessEntityID = p.VendorID  
  13.            WHERE v.CreditRating = 5  
  14.           )  
  15. BEGIN  
  16. RAISERROR ('A vendor''s credit rating is too low to accept new  
  17. purchase orders.', 16, 1);  
  18. ROLLBACK TRANSACTION;  
  19. RETURN  
  20. END;  
  21. GO  
  22.  
  23. -- This statement attempts to insert a row into the PurchaseOrderHeader table  
  24. -- for a vendor that has a below average credit rating.  
  25. -- The AFTER INSERT trigger is fired and the INSERT transaction is rolled back.  
  26.  
  27. INSERT INTO Purchasing.PurchaseOrderHeader (RevisionNumber, Status, EmployeeID,  
  28. VendorID, ShipMethodID, OrderDate, ShipDate, SubTotal, TaxAmt, Freight)  
  29. VALUES (  
  30. 2  
  31. ,3  
  32. ,261  
  33. ,1652  
  34. ,4  
  35. ,GETDATE()  
  36. ,GETDATE()  
  37. ,44594.55  
  38. ,3567.564  
  39. ,1114.8638 );  
  40. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement