views:

36

answers:

1

I think it's a basic SQL question...sorry..new to it... :-)

I want to execute a SP inside which

{

I want to execute a SP (using EXEC) and depending on it's result I want to execute another query (INSERT) which if fails for any reason should revert back what the SP has done?

}

How can I do this?

Transactions? If yes, how?

PLEASE NOTE THAT THE INNER SP SENDS A MAIL.

+4  A: 

Something like this:

BEGIN TRAN

DECLARE @rc int

EXEC @rc = EXEC inner_proc

IF (@rc = 1)
   INSERT something

IF @@ERROR <> 0
    ROLLBACK
ELSE
    COMMIT
David M
What if the inner_proc sends mail to someone?? Would it get rollbacked too??
Manish
Only if you have a transaction mail server...
David M
Seriously, though, the best way to handle this would probably be to queue mails in a database table and have a background process load them up and send them. Then the insert for the mail would be rolled back in this scenario.
David M