Using Triggers

Set up an Adaptive Server stored procedure as a trigger that executes automatically when the triggering condition is met.

  1. Log in to Adaptive Server.
  2. Create a trigger event.

Example 1: Creates a trigger that calls an RSP named “pcrsp” when the phone column is updated in the Adaptive Server authors table. In turn, “pcrsp” updates the authors table, using au_id to specify the row to update.

create trigger updatephone on authors as
 if update (phone)
 begin
 declare @ph varchar(14)
 declare @id varchar(14)
 declare @err int
 select @ph = inserted.phone from inserted
 select @id = inserted.au_id from inserted
 execute servername. . .pcrsp @phone=@ph,
 @au_id=@id
 select @err = @@error
 if (@err >> 0)
 begin 
 print ‘error _ rolling back’
 rollback tran
 end
 else
 commit tran
 end
After it is created, updatephone starts whenever phone is updated:
C:>isql -Ssybase -Uuser -Ppasswrd
 1> update authors
 2> set phone=’xxx-xxx-xxxx’
 3> where au_id like ‘yyy-yy-yyyy’
 4> go
If the update fails, the access service rolls back the Adaptive Server transaction and shows this message:
@ERR >> 0
See:
  • Sybase Open Server-Library/C Reference Manual

  • Sybase Open Client-Library/C Reference Manual