Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 ?Err Handler (Transaction Commit \ Rollback)

Author  Topic 

grembos
Starting Member

7 Posts

Posted - 2011-12-13 : 22:51:44
Does this pseudo example look correct.
Suggestions or feedback welcome.... Thanks in advance
------------------
CREATE PROCEDURE usp_hdr_detail_error_hdlr @id char(1) AS

DECLARE @err int,

BEGIN TRANSACTION

-- calling another stored proc here; lets capture if its a Pass or a Fail
EXEC @err = USP_some_other_sp @param1
SELECT @err = coalesce(nullif(@err, 0), @@error) IF @err <> 0 GOTO Fail

INSERT hdr_tbl (...)
SELECT @err = @@error IF @err <> 0 GOTO Fail

UPDATE hdr_tbl
SET status = 'HDR_OK'
WHERE id = @id
SELECT @err = @@error IF @err <> 0 GOTO Fail


INSERT dtl_tbl (...)
SELECT @err = @@error IF @err <> 0 GOTO Fail

UPDATE dtlhdr_tbl
SET status = 'DTL_OK'
WHERE id = @id
SELECT @err = @@error IF @err <> 0 GOTO Fail


COMMIT TRANSACTION


Fail:
ROLLBACK TRANSACTION
END

RETURN @err

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-13 : 23:15:27
You'll need to add a RETURN after the COMMIT, otherwise it'll do the COMMIT and then it'll error on the ROLLBACK.

Is USP_some_other_sp properly coded to get you @err?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-13 : 23:15:52
I'd recommend using TRY/CATCH + transaction instead.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 23:16:24
And to add to that you've not declared @param1 anywhere in procedure

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

grembos
Starting Member

7 Posts

Posted - 2011-12-13 : 23:35:10

Doesn't the FAIL: code block only get run when called with
the GOTO Fail portion and skipped otherwise ?? .. thanks again


****extracted from the original post....
UPDATE dtlhdr_tbl
SET status = 'DTL_OK'
WHERE id = @id
SELECT @err = @@error IF @err <> 0 GOTO Fail


COMMIT TRANSACTION


Fail:
ROLLBACK TRANSACTION
END

RETURN @err

quote:
Originally posted by tkizer

You'll need to add a RETURN after the COMMIT, otherwise it'll do the COMMIT and then it'll error on the ROLLBACK.

Is USP_some_other_sp properly coded to get you @err?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

grembos
Starting Member

7 Posts

Posted - 2011-12-13 : 23:36:44
Just pseudo code for that part... thanks for you input though... appreciated

quote:
Originally posted by visakh16

And to add to that you've not declared @param1 anywhere in procedure

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 23:59:35
quote:
Originally posted by grembos


Doesn't the FAIL: code block only get run when called with
the GOTO Fail portion and skipped otherwise ?? .. thanks again


****extracted from the original post....
UPDATE dtlhdr_tbl
SET status = 'DTL_OK'
WHERE id = @id
SELECT @err = @@error IF @err <> 0 GOTO Fail


COMMIT TRANSACTION


Fail:
ROLLBACK TRANSACTION
END

RETURN @err

quote:
Originally posted by tkizer

You'll need to add a RETURN after the COMMIT, otherwise it'll do the COMMIT and then it'll error on the ROLLBACK.

Is USP_some_other_sp properly coded to get you @err?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




Nope it will continue with fail block too without RETURN

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -