Solved

Calling a Stored Procedure From a Trigger

Posted on 2006-07-10
5
292 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now