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: Monday, January 21, 2013 11:50 PM
Points: 267, Visits: 695
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, March 26, 2013 8:41 AM
Points: 2,562, Visits: 3,451
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----------
While 1 = 1 (Learning SQL....)
Click to get fast response of your post
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: Monday, January 21, 2013 11:50 PM
Points: 267, Visits: 695

[/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, March 26, 2013 8:41 AM
Points: 2,562, Visits: 3,451
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----------
While 1 = 1 (Learning SQL....)
Click to get fast response of your post
Post #1409798
Posted Tuesday, January 22, 2013 5:03 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 7:19 AM
Points: 345, Visits: 1,322
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;



http://thesqlguy.blogspot.com/
Post #1409955
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse