Advertisement
COMPUTING_Memes

Untitled

Apr 2nd, 2021
4,393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.41 KB | None | 0 0
  1. ALTER TRIGGER CustomerProtect
  2. ON tblCustomer
  3. AFTER UPDATE
  4. AS
  5. --Checks if a customer address has been provided in the update statement.
  6. IF EXISTS (SELECT CustomerAddress FROM inserted WHERE CustomerAddress = '')
  7. BEGIN
  8.     PRINT ('Error: Please provide a Customer Address to allow this update to continue.')
  9.     ROLLBACK TRANSACTION
  10. END
  11. --Checks if a customer postcode has been provided in the update statement.
  12. ELSE
  13. IF EXISTS (SELECT CustomerPostcode FROM inserted WHERE CustomerPostcode = '')
  14. BEGIN
  15.     PRINT ('Error: Please provide a Customer Postcode to allow this update to continue.')
  16.     ROLLBACK TRANSACTION
  17. END
  18. --Prevents the customer names from being updated.
  19. ELSE
  20. IF EXISTS (SELECT CustomerName FROM inserted WHERE CustomerName <> (SELECT CustomerName FROM deleted)) --WHERE CustomerName = ' ') --<> (SELECT CustomerName FROM deleted))
  21. BEGIN
  22.     PRINT ('Error Names cannot be updated.')
  23.     ROLLBACK TRANSACTION
  24. END
  25. ELSE
  26. BEGIN
  27. --Inserts the old data into the Audit table.
  28.     INSERT INTO tblNewAudit ([Identification], [DATE], [CustName], [CustomerAddressOld],[CustomerPostcodeOld])
  29.     SELECT USER, CURRENT_TIMESTAMP, CustomerName, CustomerAddress, CustomerPostcode FROM deleted
  30. --Updates the number of previous addresses column in the customer table.
  31.     UPDATE tblCustomer SET [PreviousAddressNumber] += '1' WHERE CustomerName = (SELECT CustomerName FROM deleted)  
  32.     SELECT * FROM [dbo].[tblNewAudit]
  33.     PRINT ('COMPLETED')
  34. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement