SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TRY - CATCH with multiple errors


TRY - CATCH with multiple errors

Author
Message
Dave Mason
Dave Mason
Mr or Mrs. 500
Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)

Group: General Forum Members
Points: 544 Visits: 901
If I open a new window in SSMS, attempt to backup a database, and the operation fails, I get two error messages. Here's a simple example that attempts to backup a database snapshot:

BACKUP DATABASE MyDbSnapshot
TO DISK = 'MyDbSnapshot.bak'



Msg 3002, Level 16, State 1, Line 1
Cannot BACKUP or RESTORE a database snapshot.
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.


If I try the same db backup in a TRY-CATCH block, I lose the first (and more pertinent) error message:

BEGIN TRY
BACKUP DATABASE MyDbSnapshot
TO DISK = 'MyDbSnapshot.bak'
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
END CATCH



Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.


How do you access all of the errors in the CATCH block?

Dave MasonSeminole County, FL
Roy Ernest
Roy Ernest
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3940 Visits: 6869
The try catch will catch only the last message since usually the first message gives the information and the last one gives the final result (Terminating message).
One thing you could try is to call this from a power shell and pipe the output to a file or something.
That is the only way I can think of.

-Roy
GSquared
GSquared
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24859 Visits: 9730
As per its documentation, Try Catch only gets the error that passes control to the Catch block. The only way to do what you want is to do it outside of a Try Catch block.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Grant Fritchey
Grant Fritchey
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43061 Visits: 32667
Also CATCH won't catch syntactical (is that a word) errors, so you may see two errors in fact, but you'll only catch one

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Dave Mason
Dave Mason
Mr or Mrs. 500
Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)

Group: General Forum Members
Points: 544 Visits: 901
Dave Mason (10/8/2010)
Msg 3002, Level 16, State 1, Line 1
Cannot BACKUP or RESTORE a database snapshot.
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.



Does SQL Server record the above error messages anywhere? (System table? Other?)
It does not appear to be in the error log. Rather, I see the following, which (again) is not pertinent:
BACKUP failed to complete the command BACKUP DATABASE MyDbSnapshot. Check the backup application log for detailed messages.

Dave MasonSeminole County, FL
Derrick Smith
Derrick Smith
SSC Eights!
SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)

Group: General Forum Members
Points: 960 Visits: 715
In the step of the job on the Advanced tab, specify an output file (.txt). It will log all errors to that.

You should also see more detail in the step of the job history, but that might be truncated. The output file does not get truncated.
Dave Mason
Dave Mason
Mr or Mrs. 500
Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)

Group: General Forum Members
Points: 544 Visits: 901
Derrick Smith (10/12/2010)
In the step of the job on the Advanced tab, specify an output file (.txt). It will log all errors to that.


Thanks, Derrick. That helps some.

Ideally, what I want to do is have my SQL Job create backups for multiple db's. If a backup for a single db fails, I want to capture all the error messages (including the first error message that actually tells me why it failed), and then proceed to the next db backup.

Oh, and I would prefer to do everything in tsql. (Just a personal preference.) I don't have any SSIS packages and I don't use the built-in maintenance plans--I'd like to keep it that way. (Scathing replies may commence now...)

Dave MasonSeminole County, FL
Dave Mason
Dave Mason
Mr or Mrs. 500
Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)

Group: General Forum Members
Points: 544 Visits: 901
BTW,
I found this related submission on MS Connect: Incorrect error number passed to catch block

Please go there and vote for it as an important issue. (You'll have to login with an MS Live account.)

Maybe MS will address it in an upcoming release or service pack...

Dave MasonSeminole County, FL
Dave Mason
Dave Mason
Mr or Mrs. 500
Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)Mr or Mrs. 500 (544 reputation)

Group: General Forum Members
Points: 544 Visits: 901
Here's a pure t-sql solution to my problem:PJ on Development: Retrieve Error Message

Dave MasonSeminole County, FL
manub22
manub22
SSC Veteran
SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)

Group: General Forum Members
Points: 281 Visits: 1863
As this is very old thread.

But just updating this with the solution that the new keyword THROW introduced in SQL Server 2012 will serve the purpose.
THROW in CATCH block will return multiple errors encountered in TRY block.

Check my blog post here: http://sqlwithmanoj.com/2013/01/24/another-reason-to-use-throw-clause-instead-of-raiserror-sql-server-2012/


~manoj | LinkedIn | Facebook | YouTube
Blog: SQLwithManoj.com

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