January 24, 2011 at 5:46 pm
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.
January 24, 2011 at 7:02 pm
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.:-)
January 25, 2011 at 2:24 pm
Sure.
But how do you trap these kinds of errors!
January 26, 2011 at 7:24 am
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.
January 26, 2011 at 4:27 pm
ok, thanks.
January 27, 2011 at 2:10 am
January 27, 2011 at 4:03 am
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.
January 27, 2011 at 10:22 am
I echo tertiusdp's comments.
January 27, 2011 at 9:55 pm
(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; Theyll drag you down to their level and beat you with experience
January 28, 2011 at 1:36 am
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.
July 18, 2012 at 8:06 am
I'm running into the same scenario, except I'm not using a Try/Catch...here's my code:
Exec sp_changedbowner 'sa'
If (@@Error <> 0)
Print 'errored changing owner'
I get an error level 16 as well, but the @@Error <> 0 block never fires....
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply