From the course: SQL Server 2014: Developing Databases
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Utilizing triggers - SQL Server Tutorial
From the course: SQL Server 2014: Developing Databases
Utilizing triggers
- Ok, the first thing we're going to do is in AdventureWorks 2014, expand HumanResources.Employee table and expand the triggers folder DEmployee, I'm going to right-click that, Script Trigger as, Create to, New Query Editor. - Let's take a look at a couple triggers that exist in AdventureWorks 2014. And here I'm looking in HumanResourcesEmployee under Triggers, I've got DEmployee Trigger. Now if you look, after our naming, this is an Instead Of Trigger, so Instead Of Delete, and Not For Replication, meaning if we're replicating the database, this won't be replicated. So, Instead Of Delete, and we're declaring some variables. Then we come down here and we see the interesting part of the code which is after the Begin. So, on Instead of firing at delete, this block of code is going to execute. We're going to raise an error and it says, if you read this, you can't delete an employee, you can only mark them as not current. So, Raise an Error, Passback, Begin Rollback Transaction. You see a…