Solved

Calling a Stored Procedure From a Trigger

Posted on 2006-07-10
5
294 Views
Last Modified: 2006-11-18
Is it possible to call a stored procedure from a trigger in TQL? If so, what are the implications of doing this while using transactions?

If a transaction is rolled back in a stored procedure is there any way of detecting this in the trigger?

Thanks.
0
Comment
Question by:AMLabels
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 17074182
this is possible, the transaction will englobe the stored procedure.
if the procedure rolls back the transaction, the tigger will also rollback everything.


0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17075212
You may also have to call the Stored Procedure in a CURSOR, one time for each row added and / or deleted.
0
 
LVL 12

Expert Comment

by:Einstine98
ID: 17076901
The way to call a stored procedure is

EXEC storedprocedurename

acperkins is right, however, I would personally try as much as possible avoid using a stored procedure call in a trigger... many complications (coding wise) including the use of a cursor or having to stage the INSERTED table...
0
 

Author Comment

by:AMLabels
ID: 17079589
What do you mean by 'stage' the INSERTED table?

I'm still wondering about the use of transactions within the trigger and the subsequently called sp.

If this is the TRIGGER:

BEGIN TRIGGER

    BEGIN TRANSACTION

        CALL STORED PROCEDURE
        ADDITIONAL TRANSACTION STATEMENTS ...

     COMMIT TRANSACTION

END TRIGGER

How would it be possible to rollback this transaction in the stored procedure? Is the only way to do this by having the return value of the sp indicate it's success and then rollback inside the trigger?

Or could the sp just call the ROLLBACK statement without it's own BEGIN and END TRANSACTION block..? Is this what was meant by englobing?

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17258812
if the stored procedure uses ROLLBACK, then in the trigger check @@TRANCOUNT  
if it is 0, the transaction got rolled back in the procedure
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question