Now is possible SELECT the table TBLERRORLOG with a certain filter:
SELECT*FROM TBLERRORLOG where DETAIL like'%master%'
If you have several error logs, you can use the following code:
truncatetable TBLERRORLOG
Go
INSERTINTO TBLERRORLOGexecsp_readerrorlog 2
go
SELECT*FROM TBLERRORLOG where DETAIL like'%master%'
First, the table TBLERRORLOG is truncated, then the table is filled using the undocumented stored procedure sp_readerrolog, there is possible to define the ERROLOG number, in this case I use 2, then a SELECT statement is used. This code can be used several times depending of the ERROLOG allowing us a quickly way to find some issue.
For SQL Server 2000 and earlier version, the most common way to working error as using @@ERROR function. SQL Server 2005 introduces the construct TRY…CATCH, it improves handling errors. The construct TRY…CATCH is the principal tool for handling error on SQL Server, specifically with exceptions errors. This capability allows you to manage exception errors in similar way as .NET programming languages.
A TRY…CATCH consist in two parts: these are a TRY block followed immediately by a CATCH block. All the TSQL Code must to be on TRY block and the handling error must to be on CATCH block. When in the TRY block does not exist error, the CATCH block is simply skipped. If the TRY block has an error, then the control is transferred to the next CATCH block.
Both, TRY and CATCH block starts with BEGIN TRY and BEGIN CATCH respectively, and ends with END TRY and END CATCH respectively.
You can test the following code in order to see a case without error on TRY block.
-- TSQL WORKING FINE
BEGINTRY
PRINT 100 / 10
PRINT'Working on BLOCK TRY. WITHOUT ERRORS!!!!!!'
ENDTRY
BEGINCATCH
PRINT'Working on BLOCK CATCH. WITH ERRORS!!!!!!'
ENDCATCH
The code on TRY block was executed successfully; that’s way the CATCH block was not executed. The result is the following:
10
Working on BLOCK TRY. WITHOUT ERRORS!!!!!!
Now, execute the following code with error on the TRY block:
-- TSQL WITH ERROR - DIVIDE BY ZERO
BEGINTRY
PRINT 100 / 0
PRINT'Working on BLOCK TRY. WITHOUT ERRORS!!!!!!'
ENDTRY
BEGINCATCH
PRINT'Working on BLOCK CATCH. WITH ERRORS!!!!!!'
ENDCATCH
The divide by zero causes an ERROR on the TRY block, it cause that the control was passed immediately to CATCH BLOCK and also cause that the second PRINT on TRY block was not executed. The result is the following.
Working on BLOCK CATCH WITH ERRORS!!!!!!
To obtain information about the error that cause the CATCH block to be executed, SQL Server provides the following system functions:
ERROR_NUMBER() function returns an integer with the number of error
ERROR_SEVERITY() function returns the severity number.
ERROR_STATE() function returns the error state number.
ERROR_LINE() function returns the line number inside the routine that caused the error.
ERROR_MESSAGE() function returns the complete text of the error message. The text includes the values supplied for any substitutable parameters, such as lengths, object names, or times.
ERROR_PROCEDURE() function returns the name of the stored procedure or trigger where the error occurred.
You can execute the following code in order to create a Stored Procedure that encapsulates the system functions mentioned above. Also you can customize the error messages. The following code is customized when the error number 208 (Invalid Object Name) happens, this customized message error will be used later. :
createprocedure dbo.sp_err_msg
as
if ERROR_NUMBER()= 208
print'Oops, the table queried does not exists. '
else
select
ERROR_NUMBER()as ErrorNumber,
ERROR_SEVERITY()as Severity,
ERROR_STATE()as State,
ERROR_LINE()as ErrorLine,
ERROR_MESSAGE()as ErrorMessage,
ERROR_PROCEDURE()as ErrorOnProcedure
GO
You can modify the second code used in this article in order to see how works the error system functions.
BEGINTRY
PRINT 100 / 0
PRINT'Working on BLOCK TRY. WITHOUT ERRORS!!!!!!'
ENDTRY
BEGINCATCH
exec dbo.sp_err_msg
ENDCATCH
The divide by zero cause an error on TRY block, then the control was passed to CATCH block and the stored procedure is executed. The result is the following.
Some errors are not handled by construct TRY…CATCH :
Compilation errors. Ex syntax error.
Recompilation errors. Ex object name resolution error.
If an compilation or recompilation errors occurs when they are at TRY … CATCH level, those errors are not handled by construct TRY …CATCH. On the other hand, if the error occurs at a lower execution level (ex. Sp_executesql or a stored procedure) the errors are handled by the construct TRY…CATCH.
Run the following code to demonstrate an event that is not handle by TRY…CATCH. The code, execute a SELECT statement to a non existing table, it cause an error:
BEGINTRY
SELECT*FROM TABLENOTDEFINED
ENDTRY
BEGINCATCH
exec dbo.sp_err_msg
ENDCATCH
Result:
Msg 208, Level 16, State 1, Line 2
Invalid object name 'TABLENOTDEFINED'.
However, if the SELECT statement is executed by a stored procedure the error will occur at a level lower than TRY block. Then, the error will be handled by the construct TRY … CATCH.
createprocedure dbo.sp_exec_TABLENOTDEFINED
as
SELECT*FROM TABLENOTDEFINED
GO
BEGINTRY
exec dbo.sp_exec_TABLENOTDEFINED
ENDTRY
BEGINCATCH
exec dbo.sp_err_msg
ENDCATCH
The table non defined on the Stored procedure cause an error 208, it is handled by TRY ... CATCH, and a customized error message is shown.
Result.
Oops, the table queried does notexists.
Conclusion
TRY..CATCH is the main tool provided by SQL Server to handling errors. To obtain more information about the errors is possible to use a system functions as: ERROR_NUMBER(), ERROR_SEVERITY(), ERROR_STATE(), ERROR_LINE(), ERROR_MESSAGE(), ERROR_PROCEDURE().
In a table, after a column is dropped using alter table drop column statement, is necessary to reclaim the space, just when a fixed-length column or text column were dropped.
To see the behavior you can create 3 tables with 3 fields, this 3 fields must to be different datatypes
-- create a db test
createdatabase db_test
go
use db_test
go
-- create 3 table with the same fields
createtable test1 (
achar(20),--fixed-lenght column
bvarchar(20),--variable-lenght column
ctext--text column
)
go
createtable test2 (
achar(20),--fixed-lenght column
bvarchar(20),--variable-lenght column
ctext--text column
)
go
createtable test3 (
achar(20),--fixed-lenght column
bvarchar(20),--variable-lenght column
ctext--text column
)
Go
Now, you can add some information and see the space of each table.
-- insert some information into the 3 tables.
insertinto test1 values('aaaaaaaaaa','bbbbbbbbb','text text text text text text text text text text text text text ')
insertinto test2 values('aaaaaaaaaa','bbbbbbbbb','text text text text text text text text text text text text text ')
insertinto test3 values('aaaaaaaaaa','bbbbbbbbb','text text text text text text text text text text text text text ')
--checking the space
sp_spaceused test1
Go
sp_spaceused test2
Go
sp_spaceused test3
GO
/*
--Result
test1132 KB24 KB8 KB0 KB
test2132 KB24 KB8 KB0 KB
test3132 KB24 KB8 KB0 KB
*/
Then, drop a column for each table, after drop a column the space for each table is the same than before. To reclaim the space is necessary to execute the dbcc cleantable on each table. This dbcc utility must to be executed when a variable-length column or text column were dropped.
-- drop one column in each table in order to
-- see the behavior after drop a column
altertable test1 dropcolumn a
altertable test2 dropcolumn b
altertable test3 dropcolumn c
--reclaim space
dbcc cleantable ('db_test','test1')
dbcc cleantable ('db_test','test2')
dbcc cleantable ('db_test','test3')
MSDN says “DBCC CLEANTABLE reclaims space after a variable-length column or a text column is dropped by using the ALTER TABLE DROP COLUMN statement. The command does not reclaim space after a fixed-length column is dropped.”
After execute DBCC CLEANTABLE, you can see that some space was reclaimed for the table test3 where the TEXT column was dropped, for the other two tables the space is the same, even for the table 2 where a variable-length column was dropped, in this case some space must to be reclaimed, perhaps does not happening anything because the amount of data is not relevant.
In this article we are going to see how we can work with ERRORLOG. What is the ERRORLOG? The ERRORLOG, is the SQL Server Log, it means, the place where we can check the errors for SQL Server.
SQL Server has several ERRORLOG files, by default each instance contains 6 ERROLOG files, it value can be changed and we are going to see how to do it, later. Every time that SQL Server starts a new ERRORLOG file is generated and the current ERROLOG is renamed to ERROLOG.1; and the ERRORLOG.1 is renamed to ERRORLOG.2 and so on.
How to see ERRORLOG content?
To see the ERRORLOG we can use the Microsoft SQL Server Management Studio (SSMS) or using TSQL.
Using TSQL:
We can use a extended stored procedure called xp_readerrorlog, it display the errors existing on the last ERRORLOG file.
2009-07-01 18:35:02.150 spid53Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)
Oct 14 2005 00:33:37
Copyright (c) 1988-2005 Microsoft Corporation
Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 2)
2009-07-01 18:35:02.150 spid53(c) 2005 Microsoft Corporation.
2009-07-01 18:35:02.150 spid53All rights reserved.
2009-07-01 18:35:02.150 spid53Server process ID is 1208.
2009-07-01 18:35:02.150 spid53Logging SQL Server messages in file 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\ERRORLOG'.
Using SSMS:
We can check one file or all the files mixed in the following path: SSMS->Management->SQL Server Logs.
How to see ERRORLOG files list?
To know the file name, creation date and file size for the errorlog files in my SQL Server instance, we can use the stored procedure called xp_enumrrorlogs, it does not need any input parameter.
Obviously, we can use Windows Explorer J as well. In the results of xp_readerrorlog we can see where the errolog files are located.
How ERRORLOG can be Cycling/Purge/Reset?
As is mentioned on the beginning of this article, every time that SQL Server is started the ERRORLOG is purge and a new ERRORLOG file is generated.
When a SQL Server instance is up and running for long periods of time, the errorlog file grow up considerably and it cause hard to read the entire file. SQL Server offers the capability to cycle the ERRORLOG in a manual way, to perform this task we can use the system stored procedure called sp_cycle_errorlog.
execsp_cycle_errorlog
Result.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
When sp_cycle_errorlog is executed successfully, the current ERRORLOG file is renamed to ERRORLOG.1 and the same behavior happens with the rest of ERRORLOG files. The last ERRORLOG file is deleted.
Another way to purge the SQL Server log is using the DBCC ERROLOG.This DBCC is used when the stored procedure sp_cycle_errorlog is executed.
DBCC ERRORLOG
How many old ERRORLOG are needed?
If the plan is to cycle/purge/reset the errorlog file in a manual way, is necessary keep in mind the timeframe to generate a new log file. Also is necessary to know the frequency to reset the log and the amount of information necessary to hold.
If the plan is to hold more files than the default. We can change using the SSMS->Management -> SQL Server Logs, then right click and choose the Configure option. The values allowed are from the default (6) until 99, it value is the maximum value allowed by SQL Server.
Conclusion
Having an ERRORLOG file in a reasonable size, it makes easier to browse and analyze the log error. This article show us how we can work with the errolog file: cycling the file with sp_cycle_errorlog, reading the file with xp_readerrorlog, listing the files with xp_enumerrologs and changing the total of errorlog files.
Database specialist, currently I am working with differents databases as DB6, MAXDB, Oracle and SQL Server in SAP Environments.
My expertise area and strongest skills is working with SQL Server environments.