Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

try catch blocks Expand / Collapse
Author
Message
Posted Saturday, October 16, 2010 7:34 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 06, 2014 4:32 AM
Points: 127, Visits: 350
in sql server 2005 in SP.... i have a try- catch block.... in try block based on condition can i diverted to coltrol to catch block?


begin try
begin transaction
insert stmt
if value>1
--it should rollock
commit
end try

begin catch
if @@transcount>0
rollback
end catch


or can i use rollback in try block itself?
Post #1005726
Posted Friday, September 20, 2013 6:37 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 3:21 PM
Points: 16, Visits: 33
Within the CATCH block, you can commit you can commit or roll the current transaction unless the transaction cannot be committed and must be rolled back. To test for the state of the transaction, you can query the EXACT_STATE function.

System going down at 5 this afternoon to install scheduler bug.
Post #1496848
Posted Friday, September 20, 2013 8:20 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:00 AM
Points: 11,933, Visits: 10,970
You can have a rollback inside the try block.

begin try
begin transaction
insert stmt
if value>1
rollback --Or you could use RAISERROR
--Raising an error might be a better approach if you want to log that error or do something with it
else
commit
end try

begin catch
if @@transcount>0
rollback
end catch



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1496911
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse