Jump to content

INSERT trigger


sql_learner

Recommended Posts

Hello, I am trying to create an insert trigger for the first time and have some difficulty getting the trigger below to work. I am using MS SQL Server 2008 R2 I want to create an INSERT trigger that will prevent the adding of a row with a province that is not BC. Essentially if the province is BC the trigger should allow the update to happen, if the province is not BC the trigger should prevent the row from being added. I know I need to use the inserted virtual table, but I am not sure what I am doing wrong. Any help would be very appreciated, thanks! // code to testinsert Employeesvalues (10, 'Brown', 'Joe', '21 Jump Street', 'Vancouver', 'BC', 'V6R 5Z4', '6045555555','1981-07-23') insert Employeesvalues (11, 'Brown', 'Joe', '21 Jump Street', 'Vancouver', 'ON', 'V6R 5Z4', '6045555555','1981-07-23') go //INSERT TriggerCREATE TRIGGER tr_insert_employeeON Employeesfor insertasif (select province from inserted) <> 'BC' BEGINPRINT 'record will not update if province is not BC'ROLLBACK TRANSACTION END

Edited by sql_learner
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...