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

Execute statement in CATCH block Expand / Collapse
Author
Message
Posted Monday, January 21, 2013 10:54 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 2:21 AM
Points: 271, Visits: 722
Hi,

Need some help in determining if the below statement is good enough.
How can I get statement to execute in Catch block?
I'm out of ideas..


IF (SELECT is_broker_enabled FROM sys.databases WHERE name = 'MYDB') = 0
BEGIN TRY
ALTER DATABASE MYDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE MYDB SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE
ALTER DATABASE MYDB SET MULTI_USER;
END TRY
BEGIN CATCH
ALTER DATABASE MYDB SET NEW_BROKER

END CATCH
Post #1409787
Posted Monday, January 21, 2013 11:07 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 12:26 AM
Points: 2,840, Visits: 3,963
khushbu (1/21/2013)
Hi,

Need some help in determining if the below statement is good enough.
How can I get statement to execute in Catch block?
I'm out of ideas..


IF (SELECT is_broker_enabled FROM sys.databases WHERE name = 'MYDB') = 0
BEGIN TRY
ALTER DATABASE MYDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE MYDB SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE
ALTER DATABASE MYDB SET MULTI_USER;
END TRY
BEGIN CATCH
ALTER DATABASE MYDB SET NEW_BROKER

END CATCH
Do you want that whenever ANY try block get failed due to any reason , the stmt in catch get fired ? i dont think so. Why can't you use IF-ELSE block, Catch is generally use to handle errors.


-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1409788
Posted Monday, January 21, 2013 11:19 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 2:21 AM
Points: 271, Visits: 722

[/quote]Do you want that whenever ANY try block get failed due to any reason , the stmt in catch get fired ? i dont think so. Why can't you use IF-ELSE block, Catch is generally use to handle errors.
[/quote]

IF-ELSE block is working. But I would like try the statements in TRY-CATCH block, while there are some additional statements in CATCH block to report.
Post #1409794
Posted Monday, January 21, 2013 11:26 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 12:26 AM
Points: 2,840, Visits: 3,963
khushbu (1/21/2013)
IF-ELSE block is working. But I would like try the statements in TRY-CATCH block, while there are some additional statements in CATCH block to report.
Catch is generally used for error handling and logging .


Lets take an example , the database on which you are trying to set service_broker doesnt exist , in that case your catch block will also get FAILED . think ? .

So better redesign/change your business logic


-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1409798
Posted Tuesday, January 22, 2013 5:03 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 7:52 AM
Points: 906, Visits: 2,856
TRY CATCH is used for error handling while IF is used for conditional logic.

IF (Some Condition)
BEGIN
-- run these statements if your condition is true
END
ELSE BEGIN
-- run these statements if your condition is false
END;

BEGIN TRY
-- run these statements
END TRY

BEGIN CATCH
-- only run these statements if an error occurred above
END CATCH;





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Post #1409955
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse