Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T SQL Error Capturing


T SQL Error Capturing

Author
Message
mymail.default
mymail.default
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 258
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.
ColdCoffee
ColdCoffee
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2319 Visits: 5545
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.:-)
mymail.default
mymail.default
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 258
Sure.

But how do you trap these kinds of errors!
tertiusdp
tertiusdp
Old Hand
Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)

Group: General Forum Members
Points: 359 Visits: 244
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.
mymail.default
mymail.default
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 258
ok, thanks.
VSSGeorge
VSSGeorge
Say Hey Kid
Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)

Group: General Forum Members
Points: 685 Visits: 1422
Hi
Refer this link

http://beyondrelational.com/blogs/ashish/archive/2009/05/09/error-exception-handling-in-sql-server-2005.aspx
tertiusdp
tertiusdp
Old Hand
Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)

Group: General Forum Members
Points: 359 Visits: 244
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.
mymail.default
mymail.default
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 258
I echo tertiusdp's comments.
MyDoggieJessie
MyDoggieJessie
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4911 Visits: 7365
(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" ;-)
tertiusdp
tertiusdp
Old Hand
Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)Old Hand (359 reputation)

Group: General Forum Members
Points: 359 Visits: 244
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.
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