Wednesday, March 7, 2012

Error Log Management in SQL Server

There are few system stored procs which helps us to manage the error logs in SQL Server

1. msdb.dbo.sp_cycle_errorlog
2. msdb.dbo.sp_cycle_agent_errorlog
3. xp_readerrorlog

xp_readerrorlog  To view current error log, This SP takes 4 parameters
1. 0 – Current Log file, 1 (Archive#1), 2(Archive#2)
2. 1, 2 (1 Indicates SQL Server, 2 Indicates SQL Agent)
3. Search Text
4. Refine Search with in the search
5. StartDate
6. EndDate
7. Asc | Desc

Examples
xp_readerrorlog 0            -- Reads SQL Server Error Log
xp_readerrorlog -1,2        -- Reads SQL Server Agent Log
xp_readerrorlog 0,1, "Logging SQL Server messages in file"  This command helps to identify the location of error log file.

This above system stored procedure helps us to create a new error log file without restarting the SQL Service & SQL Agent Service

This will creates a new error log file from the point when you execute it. It requires sysadmin role to execute.

Script level upgrade for database 'master' failed because upgrade step 'sqlagent100_msdb_upgrade.sql' encountered error 598, state 1, severity 25. This is a serious error condition which might interfere with regular operation and the database will be taken offline. If the error happened during upgrade of the 'master' database, it will prevent the entire SQL Server instance from starting. Examine the previous errorlog entries for errors, take the appropriate corrective actions and re-start the database so that the script upgrade steps run to completion

I encountered the below errors in SQL Error Log after completion of service pack / cumulative update on SQL Server 2008. Below are my work around to fix the issue
Error: 3417, Severity: 21, State: 3.

Cannot recover the master database. SQL Server is unable to run. Restore master from a full backup, repair it, or rebuild it. For more information about how to rebuild the master database, see SQL Server Books Online.
Cannot create file "\MSSQL10.\MSSQL\Data\temp_MS_AgentSigningCertificate_database.ldf' because it already exists. Change the file path or the file name, and retry the operation.
SQL Trace was stopped due to server shutdown. Trace ID = '1'. This is an informational message only; no user action is required.


Error: 1802, Severity: 16, State: 1.

CREATE DATABASE failed. Some file names listed could not be created. Check related errors.


WorkAround
1. Firstly we need to verify the SQL Server has enough permissions on data and log folders.
2. Need to verify the below registry Key Default Data Path is >> HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.\Setup\SQLDataRoot
If this key has invalid path we can update the path and restart the SQL Server will solve the issue, if it has valid path and it still showing the error then
We need to move the temp_MS_AgentSigningCertificate_database.ldf & mdf to some other location and try to bring the service up from the cluster manager. Hope it will resolve the issue.


Tuesday, March 6, 2012

How to rename a database along with physical & logical files




Select * from SourceDB..sysfiles


ALTER DATABASE SourceDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE SourceDB SET MULTI_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE SourceDB MODIFY Name = TargetDB
ALTER DATABASE TargetDB MODIFY FILE (NAME=N'SourceDB', NEWNAME=N'TargetDB')
ALTER DATABASE TargetDB MODIFY FILE (NAME=N'SourceDB_log', NEWNAME=N'TargetDB_log')


USE [master]
ALTER DATABASE TargetDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
EXEC master.dbo.sp_detach_db @dbname = N'TargetDB'
Rename Physical files as per the ‘TargetDB’


USE [master]
CREATE DATABASE TargetDB ON
( FILENAME = N'F:\DataFiiles\TargetDB_data.mdf'),
( FILENAME = N'G:\LogFiles\TargetDB_log.ldf')
FOR ATTACH


select * from TargetDB..sysfiles