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

Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

Embed Size (px)

DESCRIPTION

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

Citation preview

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

Copyright © 2013 Curt Hill

Triggers

The Generation of Indirect Actions

Page 2: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

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

Page 3: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

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

Page 4: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

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

Page 5: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

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

Page 6: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

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

Page 7: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

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

Page 8: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

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

Page 9: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

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

Page 10: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

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

Page 11: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

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

Page 12: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

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

Page 13: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

Example• In the next screens is a trigger to

show the new value of an update to the student balance

Copyright © 2013 Curt Hill

Page 14: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

Example 1

Copyright © 2013 Curt Hill

create trigger bal_increaseon studentsafter update as begin declare @count int, @bal decimal(8,2) set @count = 0 declare curses Cursor for select s_balance from inserted open curses

Page 15: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

Example 2

Copyright © 2013 Curt Hill

fetch next from curses into @balwhile (@@FETCH_STATUS >= 0) begin set @count = @count + 1 print cast (@bal as varchar(20)) fetch next from curses into @bal enddeallocate cursesend

Page 16: Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions

Demo• Perhaps we should do a demo?

Copyright © 2013 Curt Hill