Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TRANS Split over 2 Stored Procs

Status
Not open for further replies.

hegartyjp

Programmer
Jun 16, 2004
57
US
Hi,

Hoping someone could please help.

I have 2 stored procedures that run one after the other.

They both insert a row but in 2 seperate tables.

However the rows are linked so if the row goes in the first table, it has to also go in the second table.

I know how to use Transactions within a stored procedure but is there anyway to run one SP, then run the second but if the second one fails, roll back to before the first sp?

Any help greatly appreciated

Thanks

JP
 
Thanks.

But to wrap them in one TRAN do I need to have them all in one procedure?

Regards

JP
 
play around with this

Code:
SET XACT_ABORT ON
BEGIN TRAN VooDoo
DECLARE @err int

EXEC @err = some_other_sp 
SELECT @err = coalesce(nullif(@err, 0), @@error)
IF @err <> 0 
BEGIN 
	ROLLBACK TRANSACTION  
END

EXEC @err = some__yet_another_sp 
SELECT @err = coalesce(nullif(@err, 0), @@error)
IF @err <> 0 
BEGIN 
	ROLLBACK TRANSACTION  
END

IF @err = 0 
BEGIN 
	COMMIT TRANSACTION  
END

also read:
Denis The SQL Menace
--------------------
SQL Server Code,Tips and Tricks, Performance Tuning
SQLBlog.com, Google Interview Questions
 
Thanks fella.

I will try this and see how I get on.

Appreciate the help

JP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top