Solved

SQL TRY/CATCH testing for existence.

Posted on 2011-09-02
2
224 Views
Last Modified: 2012-06-21
I am reading Microsoft SQL Server 2008 - Database Development book for example 70-433.

On page 155 (in the NOTE TRAPPING ERRORS) it states that "you cannot use TRY...CATCH block to test for an objects existence".

I put this to the test and the TRY did pass control to the CATCH. Here is my code. Can anybody explain this to me...Thanks.

CREATE TABLE dbo.test
(
ID INT NOT NULL PRIMARY KEY
)

BEGIN TRY
CREATE TABLE dbo.test
(
ID INT NOT NULL PRIMARY KEY
)
END TRY
BEGIN CATCH
PRINT 'Table cannot be created'
END CATCH
0
Comment
Question by:Mr_Shaw
2 Comments
 
LVL 28

Accepted Solution

by:
Ryan McCauley earned 500 total points
ID: 36473615
You're testing if an object can be created in your example, and the TRY...CATCH would handle that properly. They're referring to something liek this:

BEGIN TRY
   select * from SomeTableThatDoesntExist
END TRY
BEGIN CATCH
    PRINT 'Table does not exist'
END CATCH

Open in new window


If you execute that, you'll get a SQL Error, not your printed error statement from the CATCH block. What they're saying is that a missing object won't pass control to the CATCH block, it will stop your script in its tracks, so you can't use TRY...CATCH for control flow in this case.
0
 

Author Closing Comment

by:Mr_Shaw
ID: 36473645
thanks
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server merge records in one table 2 24
Sql Query 4 39
replicated - directional or bidirectional? 3 29
Need help how to find where my error is in UFD 6 30
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question