Answered by:
BEGIN AND END INSIDE TRY BLOCK

Question
-
I HAVE FOLLOWING CODE STRUCTURE.
BEGIN TRY
IF EXISTS(CHECK IF VALUES EXISTS)
BEGIN
DO SOMETHING
END
END TRY
BEGIN CATCH
LOG ERRORS INTO TABLE
END CATCH
with the above structure. If i just run if exists it is wokring fine. If i run whole code it keeps on running and it never stops. Not sure if there is something wrong with code. Let me know if this is the correct way or if there is better approach for this.
Thanks in advance
Thursday, August 29, 2013 6:34 PM
Answers
-
First of all you can implement this without the BEGIN TRY\ END TRY blocks.. I dont get what is meant by (CHECK IF VALUES EXISTS) ?
IF EXISTS , should have some subquery to test..otherwise EXISTS the following code block works
DECLARE @C INT=10 BEGIN TRY IF @C='FF' BEGIN PRINT 'RIGHT' END END TRY BEGIN CATCH PRINT 'VALUE IS DIFFERENT' END CATCH
Thanks, hsbal
- Proposed as answer by Allen Li - MSFT Saturday, August 31, 2013 12:12 AM
- Marked as answer by SQL Insane Saturday, August 31, 2013 12:42 AM
Thursday, August 29, 2013 7:31 PM -
I don't see any problem with the code you posted. Based on your symptoms that the script keeps running forever, I suspect either blocking, a suboptimal query in need of query or index tuning, or a loop.
Dan Guzman, SQL Server MVP, http://www.dbdelta.com
- Proposed as answer by Allen Li - MSFT Saturday, August 31, 2013 12:12 AM
- Marked as answer by SQL Insane Saturday, August 31, 2013 12:42 AM
Thursday, August 29, 2013 11:52 PM
All replies
-
It's fine. The standard Error Handling in T-SQL is TRY/CATCH block. For more info please see this article:
Error Handling in SQL 2005 and Later
The most important motivation for the research work that resulted in the relational model was the objective of providing a sharp and clear boundary between the logical and physical aspects of database management. - E. F. Codd
My blogThursday, August 29, 2013 6:44 PM -
First of all you can implement this without the BEGIN TRY\ END TRY blocks.. I dont get what is meant by (CHECK IF VALUES EXISTS) ?
IF EXISTS , should have some subquery to test..otherwise EXISTS the following code block works
DECLARE @C INT=10 BEGIN TRY IF @C='FF' BEGIN PRINT 'RIGHT' END END TRY BEGIN CATCH PRINT 'VALUE IS DIFFERENT' END CATCH
Thanks, hsbal
- Proposed as answer by Allen Li - MSFT Saturday, August 31, 2013 12:12 AM
- Marked as answer by SQL Insane Saturday, August 31, 2013 12:42 AM
Thursday, August 29, 2013 7:31 PM -
First of all you can implement this without the BEGIN TRY\ END TRY blocks.. I dont get what is meant by (CHECK IF VALUES EXISTS) ?
The most important motivation for the research work that resulted in the relational model was the objective of providing a sharp and clear boundary between the logical and physical aspects of database management. - E. F. Codd
My blogThursday, August 29, 2013 7:45 PM -
I don't see any problem with the code you posted. Based on your symptoms that the script keeps running forever, I suspect either blocking, a suboptimal query in need of query or index tuning, or a loop.
Dan Guzman, SQL Server MVP, http://www.dbdelta.com
- Proposed as answer by Allen Li - MSFT Saturday, August 31, 2013 12:12 AM
- Marked as answer by SQL Insane Saturday, August 31, 2013 12:42 AM
Thursday, August 29, 2013 11:52 PM -
This issue has been solved. There was some issue with my subquery. I fixed that and it started working. Thanks all.Saturday, August 31, 2013 12:42 AM