|
|
|
SSC 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
|
|
|
|
|
SSCrazy
      
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
|
|
|
|
|
SSC 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.
|
|
|
|
|
SSCrazy
      
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
|
|
|
|
|
Old 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/
|
|
|
|