Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions.

Similar presentations


Presentation on theme: "Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions."— Presentation transcript:

1 Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

2 Introduction A trigger is just what you might expect – a condition that generates an action The process goes like this: –An action occurs Usually an insert/delete/update but may be many other things as well –A condition changes value –An event handler is run to handle the current situation Copyright © 2013 Curt Hill

3 Reorder Point Consider a warehouse Bins of products Database that monitors Each product has a reorder point If the number drops below that threshold the product should be reordered Any update that reduces the count could trigger a reorder –Most updates will not, but a few will Copyright © 2013 Curt Hill

4 SQL The presence of triggers makes our database an active database –Active in that the update that got things started changes the value, but makes no provision for the trigger or its actions First real standard was SQL 99 Create Trigger is the SQL command Copyright © 2013 Curt Hill

5 The Pieces Triggers conform to the Event- Condition-Action model (ECA) The Event is what gets things rolling –Usually an Update or similar statement –The event specifies table and field The Condition is optional –Is it any change or changes that provide a certain result? The Action is what to do –Stored procedure or other set of SQL statements Copyright © 2013 Curt Hill

6 SQL Server Has several separate triggers One for Insert, Update, Delete –DML triggers –These usually enforce some business rule or integrity rule One for Create, Alter, Drop, Grant, Deny, Revoke –DDL triggers are usually oriented towards security One for Logon Today we are only interested in the first Copyright © 2013 Curt Hill

7 Create Trigger Form: Create Trigger name On { table | view } { After | Instead of} {[Insert], [Update],{Delete]} As statements Next we look at what this means Copyright © 2013 Curt Hill

8 In Detail The trigger needs to be named so that it may be dropped or altered –Normal SQL names We must specify either a table or view name We may either do something after the statement executes or instead of the statement executing We must specify at least one type of statement –We may also specify any two or all three Copyright © 2013 Curt Hill

9 After What? When After is the option the statement must successfully complete –All the integrity checks must have finished Any cascaded things must also complete If the statement or started cascaded statements fail there is nothing to trigger Copyright © 2013 Curt Hill

10 Instead Of If this is given then the trigger fires up a replacement –The original statement is replaced by what the trigger specifies Recursion is disallowed –An instead of trigger cannot fire itself Copyright © 2013 Curt Hill

11 The SQL Statements Most of SQL statements may be used Typically a Begin End pair wraps them To implement a condition an If is usually the only statement –The action is accomplished only if the if condition is true Copyright © 2013 Curt Hill

12 Conceptual tables When doing inserts and deletes you may use logical tables There is a logical table inserted and deleted Inside a trigger on inserts, then this statement is legal: Select * from inserted There is no updated, but an update is like deleting the row and then inserting the updated row Copyright © 2013 Curt Hill

13 Example In the next screens is a trigger to show the new value of an update to the student balance Copyright © 2013 Curt Hill

14 Example 1 Copyright © 2013 Curt Hill create trigger bal_increase on students after update as begin declare @count int, @bal decimal(8,2) set @count = 0 declare curses Cursor for select s_balance from inserted open curses

15 Example 2 Copyright © 2013 Curt Hill fetch next from curses into @bal while (@@FETCH_STATUS >= 0) begin set @count = @count + 1 print cast (@bal as varchar(20)) fetch next from curses into @bal end deallocate curses end

16 Demo Perhaps we should do a demo? Copyright © 2013 Curt Hill


Download ppt "Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions."

Similar presentations


Ads by Google