/* Created by free online sql formatter */

CREATE TRIGGER lowcredit
ON purchasing.purchaseorderheader
AFTER INSERT
AS
  DECLARE  @creditrating TINYINT,
           @vendorid     INT
  
  SELECT @creditrating = v.creditrating,
         @vendorid = p.vendorid
  FROM   purchasing.purchaseorderheader p
         INNER JOIN inserted i
           ON p.purchaseorderid = i.purchaseorderid
         JOIN purchasing.vendor v
           ON v.vendorid = i.vendorid
  
  IF @creditrating = 5
    BEGIN
      RAISERROR ('This vendor''s credit rating is too low to accept new       purchase orders.',16,1)
      
      ROLLBACK TRANSACTION
    END