Calling a sproc for each affected record from within trigger in Sybase ASA 6
just having some headaches implementing something in a trigger on a old version of Sybase and wondered if anyone could help. In a nutshell, I want to write a trigger that will call another sproc for each of the affected records (whether it be inserted, deleted or both [updated]).
One way to do this in T-SQL (SQL Server) would be to use a cursor but none of the cursor examples I've found on the web seem to actually work on Sybase ASA 6.0.4 - it either dislikes the T-SQL in general or it appears to compile but generally does nothing (even with pathologically simple, contrived scenarios).
Essentially I want to loop through all of the 'inserted' table records (if any) and for each one Exec MySproc (inserted.keyid), then loop through all of the 'deleted' table records 开发者_如何学C(if any), and again for each one Exec MySproc (deleted.keyid).
Does anyone have an example of doing this sort of thing, or even just of using a read-only cursor in ASA 6? The online manual seems to have an example of a simple cursor but in practise I've not been able to make the example actually work. I could unroll the other sproc into the trigger (it's not that complicated really) but it's too complicated to be done as part of a 'select from inserted...' type statement - it really is a few lines of code in it's own right. Hence me thinking I need a cursor anyway.
EDIT: (29/12/09) - I ended up unrolling the sproc code, in desperation, but I really would like to have a working cursor example in Sybase ASA 6 because sooner or later I'll be faced with something I can't easily unroll. So I'll throw this up for bounty - can someone give me a working example? :-)
It sounds like you are looking for a ROW LEVEL trigger.
By default, triggers in ASA are statement level (the trigger executes once after the entire triggering statement is completed). Row level triggers, on the other hand, execute once for each row that is changed.
Here is an example of a row level trigger:
CREATE TRIGGER tr_log
AFTER UPDATE OF "myfield"
ON mytable
REFERENCING OLD AS old_data NEW AS new_data
FOR EACH ROW
BEGIN
Insert into Narc_USER_INFO_Change (Field_Changed, New_Value, Original_Value, user)
Values('myfield', new_data.myfield, old_data.myfield, CURRENT USER);
END
精彩评论