SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Execute statement in CATCH block


Execute statement in CATCH block

Author
Message
khushbu
khushbu
SSC-Addicted
SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)

Group: General Forum Members
Points: 447 Visits: 752
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
Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5272 Visits: 4076
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;-)
khushbu
khushbu
SSC-Addicted
SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)

Group: General Forum Members
Points: 447 Visits: 752
[/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.
Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5272 Visits: 4076
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;-)
Sean Pearce
Sean Pearce
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1778 Visits: 3432
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search