English 中文(简体)
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 (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
问题回答

暂无回答




相关问题
LINQ to SQL optional Column

I need a functionality to have optional columns in a LINQ to SQL definition. So that LINQ to SQL normally ignores this column within selects and updates etc. But if a select contains a value for this ...

Optional where clause / parameter in a SQL 2008 stored proc?

I m writing some code that updates a table. Depending on what the user wants to do, it either updates a large set of records, or a smaller one. The delineating factor is a group ID. The user can ...

How to control access to a Stored Procedure in SQL Server?

I want to be able to have a Stored Procedure that can only be used from a particular page, without having to create a permissions / role for a user, when it is just a single stored procedure I want ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签