Solved

Calling a Stored Procedure From a Trigger

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

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 143

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

809 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