January 21, 2013 at 10:54 pm
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
January 21, 2013 at 11:07 pm
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;-)
January 21, 2013 at 11:19 pm
[/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.
January 21, 2013 at 11:26 pm
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;-)
January 22, 2013 at 5:03 am
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;
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply