Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

T SQL Error Capturing Expand / Collapse
Author
Message
Posted Monday, January 24, 2011 5:46 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, March 27, 2014 11:01 AM
Points: 60, Visits: 226
Hello,

A quick question trying to handle an error when using sp_attach_db. Here it is in the script below with a non-existent path for the db files:

-- Start
BEGIN TRY
DECLARE @retval int;

EXEC @retval = sp_attach_db @dbname = N'myDB',
@filename1 = N'W:\myDB.mdf',
@filename2 = N'W:\myDB.ldf',
@filename3 = N'W:\myDB.ndf'

END TRY

BEGIN CATCH
print 'Here'
DECLARE @errmsg varchar(2000);
SELECT @errmsg = ERROR_MESSAGE();

END CATCH
--End

The above script does not go to the Catch block at all.

However if I were to run just this:

DECLARE @retval int;


EXEC @retval = sp_attach_db @dbname = N'myDB',
@filename1 = N'W:\myDB.mdf',
@filename2 = N'W:\myDB.ldf',
@filename3 = N'W:\myDB.ndf'

I get an error:

Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "W:\myDB.mdf" failed with the operating system error 3(The system cannot find the path specified.).

What is happening here, and how can I trap this error.

Thanks so much.
Post #1052792
Posted Monday, January 24, 2011 7:02 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, April 07, 2014 3:41 PM
Points: 2,259, Visits: 5,395
Please read about TRY CATCH block from MSDN ONline or Books Online to know about the scenarios where TRY will enter CATCH when an error is encountered. NOT ALL ERRORS are captured by TRY block.
Post #1052809
Posted Tuesday, January 25, 2011 2:24 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, March 27, 2014 11:01 AM
Points: 60, Visits: 226
Sure.

But how do you trap these kinds of errors!
Post #1053534
Posted Wednesday, January 26, 2011 7:24 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, March 13, 2014 8:51 PM
Points: 359, Visits: 243
BOL says to not use sp_attach_db - it has been deprecated.

That said, sp_attach_db and CREATE database... (replacement for sp_attach_db) is not being trapped when using a catch block.

So no closer to an answer.
Post #1053852
Posted Wednesday, January 26, 2011 4:27 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, March 27, 2014 11:01 AM
Points: 60, Visits: 226
ok, thanks.
Post #1054305
Posted Thursday, January 27, 2011 2:10 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 12:52 AM
Points: 588, Visits: 1,004
Hi
Refer this link

http://beyondrelational.com/blogs/ashish/archive/2009/05/09/error-exception-handling-in-sql-server-2005.aspx
Post #1054424
Posted Thursday, January 27, 2011 4:03 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, March 13, 2014 8:51 PM
Points: 359, Visits: 243
I read the link and I do not get how it helps. Can you explain?
Try..Catch is fine, but the problem in this case is that the catch block is not being used when there is an error.
Post #1054464
Posted Thursday, January 27, 2011 10:22 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, March 27, 2014 11:01 AM
Points: 60, Visits: 226
I echo tertiusdp's comments.
Post #1054729
Posted Thursday, January 27, 2011 9:55 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 3:33 PM
Points: 3,731, Visits: 7,069
(from MSDN)
TRY…CATCH constructs will not fire:

1. Warnings or informational messages that have a severity of 10 or lower.

2. Errors that have a severity of 20 or higher that stop the SQL Server Database Engine task processing for the session. If an error occurs that has severity of 20 or higher and the database connection is not disrupted, TRY…CATCH will handle the error.

3. Attentions, such as client-interrupt requests or broken client connections.

4. When the session is ended by a system administrator by using the KILL statement.



______________________________________________________________________________
"Never argue with an idiot; They'll drag you down to their level and beat you with experience"
Post #1055070
Posted Friday, January 28, 2011 1:36 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, March 13, 2014 8:51 PM
Points: 359, Visits: 243
What I am missing is that the error is:

Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "W:\myDB.mdf" failed with the operating system error 3(The system cannot find the path specified.).

Level 16 should be cought if I read the MSDN docs correctly.


Post #1055146
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse