Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ALTER TRIGGER CustomerProtect
- ON tblCustomer
- AFTER UPDATE
- AS
- --Checks if a customer address has been provided in the update statement.
- IF EXISTS (SELECT CustomerAddress FROM inserted WHERE CustomerAddress = '')
- BEGIN
- PRINT ('Error: Please provide a Customer Address to allow this update to continue.')
- ROLLBACK TRANSACTION
- END
- --Checks if a customer postcode has been provided in the update statement.
- ELSE
- IF EXISTS (SELECT CustomerPostcode FROM inserted WHERE CustomerPostcode = '')
- BEGIN
- PRINT ('Error: Please provide a Customer Postcode to allow this update to continue.')
- ROLLBACK TRANSACTION
- END
- --Prevents the customer names from being updated.
- ELSE
- IF EXISTS (SELECT CustomerName FROM inserted WHERE CustomerName <> (SELECT CustomerName FROM deleted)) --WHERE CustomerName = ' ') --<> (SELECT CustomerName FROM deleted))
- BEGIN
- PRINT ('Error Names cannot be updated.')
- ROLLBACK TRANSACTION
- END
- ELSE
- BEGIN
- --Inserts the old data into the Audit table.
- INSERT INTO tblNewAudit ([Identification], [DATE], [CustName], [CustomerAddressOld],[CustomerPostcodeOld])
- SELECT USER, CURRENT_TIMESTAMP, CustomerName, CustomerAddress, CustomerPostcode FROM deleted
- --Updates the number of previous addresses column in the customer table.
- UPDATE tblCustomer SET [PreviousAddressNumber] += '1' WHERE CustomerName = (SELECT CustomerName FROM deleted)
- SELECT * FROM [dbo].[tblNewAudit]
- PRINT ('COMPLETED')
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement