How to handle multiple errors - sql-server

When I run something like
BACKUP LOG [somedb]
TO DISK = N'i:\log.bak';
It throws 2 errors messages:
Msg 3201, Level 16, State 1, Line 2
Cannot open backup device 'i:\log.bak'. Operating system error 3(The system cannot find the path specified.).
Msg 3013, Level 16, State 1, Line 2
BACKUP LOG is terminating abnormally.
When I try to handle the error with a TRY CATCH the error returned is always 3013. This is a problem for me because I want to know if the backup failed due to lack of space, or if the drive isn't present, etc.
Using ##ERROR returns the same error number.
Is there any way to handle multiple error messages like these?

You need to inspect the Errors collection inside the SqlException:
catch(SqlException sqlEx)
{
foreach(SqlError error in sqlEx.Errors)
{
int code = error.Number;
string msg = error.Message;
}
}
You should get all error with all relevant details in the SqlException.Errors

Related

How to log multiple errors in TRY..CATCH?

My application runs SQL scripts to load in data and when there's a problem with the file it's loading, it only logs the last error which doesn't contain the useful information about why the file won't load.
Example code:
BEGIN TRY
BULK INSERT MyTableName
FROM 'C:\MyFilename.txt'
WITH
(
FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n',
TABLOCK,
MAXERRORS=0,
ERRORFILE = 'C:\MyFilename_Errors.log'
)
;
END TRY
BEGIN CATCH
INSERT INTO MyErrorLog
SELECT ERROR_MESSAGE() as Issue
, ERROR_LINE() as IssueRowNum
;
END CATCH
This script will create an entry in [MyErrorLog] for the third error (see below). And the log file will tell me what line, but not what field:
Row 30539 File Offset 1910820 ErrorFile Offset 0 - HRESULT 0x80004005
Here's all 3 lines if I "THROW" the error inside the CATCH:
Msg 4864, Level 16, State 1, Line 3 Bulk load data conversion error
(type mismatch or invalid character for the specified codepage) for
row 1, column 5 (MyField).
Msg 7399, Level 16, State 1, Line 3 The OLE DB provider "BULK" for
linked server "(null)" reported an error. The provider did not give
any information about the error.
Msg 7330, Level 16, State 2, Line 3 Cannot fetch a row from OLE DB
provider "BULK" for linked server "(null)".
How do I capture that first (and second) error message?
You can only capture a single error in a T-SQL CATCH block. According to this connect item, the workaround is to use THROW (or not use TRY/CATCH) and capture the errors in client code.
Consequently, you'll need to invoke the script from a client application (including SQLCLR) so that you can capture and log all errors.

How to set data and log file location for SQL Database?

I'm trying to create a database where the data and log files are saved to the E drive but not sure how to do so. I've tried:
CREATE DATABASE TachographDataContent_Archive
[ON E:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data]
[LOG ON {D:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data}]
And I'm getting this error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'TachographDataContent_Archive'.
To create database using query, you need to mention .mdf and .ldf file. So try below script.
Like this
CREATE DATABASE [TachographDataContent_Archive]
ON PRIMARY (NAME = 'TachographDataContent', FILENAME = 'E:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data\TachographDataContent.mdf')
LOG ON
(NAME = 'TachographDataContent_log', FILENAME = 'E:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data\TachographDataContent_log.ldf')
GO
good luck...

How to set SQLException Number

I'm having an issue on settin up SqlException.Number
On my Stored Proc i'm raising an error
--#number = 50001
RAISERROR(#number, 16, 1) -
I should expect that the Error_Number() should be #number but I always get 18054
Is there something wrong with my RAISERROR?
Check the sys.messages table for error code 74601. if this is a user defined error, it shouold be added in the table.
for any error that is greater than 50000 should give you this output if not found.
Msg 18054, Level 16, State 1, Line 1
Error XXXXX, severity 16, state 1 was raised, but no message with that error number was found in sys.messages. If error is larger than 50000, make sure the user-defined message is added using sp_addmessage.
There is one small caveat: You can't supply a message on your own in this case. But this can be circumvented by adding an additional %s in the sp_addmessage call or by changing all mapped messages to your own pattern and supplying the right parameters in the raiseerror call.
Check there for more information:
SQL Server: Rethrow exception with the original exception number
RAISERROR can either reference a user-defined message stored in the
sys.messages catalog view or build a message dynamically.
Check for your error message exists or not using this:
select * from sys.messages
If it does not exists then Use sp_addmessage to add user-defined error messages and sp_dropmessage to delete user-defined error messages.
for more information follow RaiseError documentation.

Restore Database and change the location for MDF File

I wont restore my database but the location path is not the same. How can i change this path(partition)?
RESTORE DATABASE [MY_DATABASE]
FROM DISK = 'C:\Content.bak'
WITH FILE = 1,
NOUNLOAD,
STATS = 10
Error Message:
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "F:....\Content01.mdf" failed with the operating system error 3(failed to retrieve text for this error.
Reason: 15100).
Msg 3156, Level 16, State 3, Line 1
File 'Content01' cannot be restored to 'F:....\Content01.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "H:....\Content01_log.LDF" failed with the operating system error 3(failed to retrieve text for this
error. Reason: 15105).
Msg 3156, Level 16, State 3, Line 1
File 'Content01_log' cannot be restored to 'H:....\Content01_log.LDF'. Use WITH MOVE to identify a valid
location for the file.
Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
THANKS.
RESTORE DATABASE [My_Database]
FROM DISK = 'C:\Content.bak'
WITH MOVE 'MyDatabase_Data' TO 'C:\Data\MyDatabase_Data.mdf',
MOVE 'MyDatabase_Log' TO 'C:\Data\MyDatabase_Log.ldf',
REPLACE,
STATS=10
Use the WITH MOVE command of the restore as discussed in this SO Question.
If anyone is here because they are restoring a database with multiple files, each destination file needs a new name. Using SQL Server 2008 R2, the gui does not provide an obvious clue nor does it solve it automatically.

SQL Server error

I am getting a fatal error on following code.
exec [sp_ExternalApp_UPDATEUSER] 'ZZZ', XXXXX','DDDDD','DDDFFDD','EREE', 'EREWWWWW',1,1,'QWEW#DFEE.DER','DEFF','XXXX','DDDD'
Following error occurred:
Location: memilb.cpp:1624
Expression: pilb->m_cRef == 0
SPID: 79
Process ID: 2256
Msg 3624, Level 20, State 1, Procedure sp_ExternalApp_UPDATEUSER, Line 32
A system assertion check has failed. Check the SQL Server error log for details
Msg 0, Level 20, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded.
Thank you
I got the solution.
I have used UPPER() function. As I removed that function, my problem solved
Like the message said, check your error log, there might be more detail in there..what does this proc do does it use sp_OACreate or calls xp_cmdshell? Post the proc code
You might want to check the database for corruption. Try
DBCC CHECKDB
(before doing so, read the documentation on that command! See http://msdn.microsoft.com/en-us/library/ms176064.aspx )

Resources