Wednesday, 16 May 2012

Configuring Deadlock Alert

/* Configuring Deadlock Alert on PRD servers.*/.

 

/* Step 1  -- Enable Trace Flag 1222 to 1 to capture deadlock information  in log files
Note: Trace flag will be reset after every SQL server reset so we need to enable flag after every SQL services restart or configure at startup parameter
*/
/* Step 2 --  Creating an Deadlock Alert and Trigger the Deadlock Job
USE [msdb]
GO

/****** Object:  Alert [DeadLock Alert]    Script Date: 05/16/2012 23:28:16 ******/
EXEC msdb.dbo.sp_add_alert @name=N'DeadLock Alert',
@message_id=0,
@severity=0,
@enabled=1,
@delay_between_responses=0,
@include_event_description_in=1,
@category_name=N'[Uncategorized]',
@performance_condition=N'SQLServer:Locks|Number of Deadlocks/sec|_Total|>|0',
@job_id=N'8a31ce05-3880-4fa4-a020-0263135d0129'
--Job ID needs to be replaced
GO


*/

/* -- Step 3.Create an new job and place an below Query in Job Step*/

/*--Create an temporary table  to capture the data from the log file */
IF OBJECT_ID('tempdb.dbo.ErrorLog') IS Not Null
BEGIN
DROP TABLE tempdb.dbo.ErrorLog
END
CREATE TABLE tempdb.dbo.ErrorLog
(Id int IDENTITY (1, 1) NOT NULL,
logdate DATETIME, procInfo VARCHAR(10),
ERRORLOG VARCHAR(MAX))
/*--Insert the data into errorlog table  from logs using SP_readerrorlog.*/
INSERT INTO tempdb.dbo.ErrorLog
EXEC master.dbo.sp_readerrorlog
/*--Query to execute the alert using DB mail */
BEGIN
DECLARE @servername nvarchar(150)
SET @servername = @@servername
DECLARE @mysubject nvarchar(200)
SET @mysubject = 'Deadlock event notification on server '+@servername+'.'
/* --Use this query in SQl 2000
--IF EXISTS(select logdate, procInfo, ERRORLOG from tempdb.dbo.ErrorLog
where Id >=(select TOP 1 Id from tempdb.dbo.ErrorLog WHERE ERRORLOG Like '%Deadlock encountered%' 
order by Id DESC))
--BEGIN
-- EXEC msdb.dbo.sp_send_dbmail
-- @profile_name ='DB Mail Profile',
-- @recipients='Deadlock@Gmail.com;Perfteam@Gmail.com ',
-- @subject = @mysubject,
-- @body = 'Deadlock has occurred. View attachment to see the deadlock info',
-- @query = 'select logdate, procInfo, ERRORLOG from tempdb.dbo.ErrorLog where Id >= (select TOP 1 Id from tempdb.dbo.ErrorLog WHERE ERRORLOG Like ''%Deadlock encountered%''  order by Id DESC)
  --   and convert(varchar,logdate,112)=convert(varchar,getutcdate(),112)',
-- @query_result_width = 600,
-- @attach_query_result_as_file = 1,
-- @importance='HIGH'

*/

/* Below query is applicable for SQL 2005 higher versions*/
IF EXISTS(
SELECT TOP 1 B.PROCINFO FROM TEMPDB.DBO.ERRORLOG B ,
(SELECT TOP 1 A.LOGDATE, A.PROCINFO FROM TEMPDB.DBO.ERRORLOG A
WHERE A.ERRORLOG LIKE '%Deadlock%' and A.ERRORLOG not like '%EXEC dbo.proc_MSS%'
--AND CONVERT(VARCHAR,A.LOGDATE,112)=CONVERT(VARCHAR,GETUTCDATE(),112)
ORDER BY A.PROCINFO DESC
)A  WHERE B.LOGDATE=A.LOGDATE AND B.PROCINFO=A.PROCINFO
)

/* Above query is used to find the deadlock in the Errolog table if deadlock exists then below DB Mail Script will be execute with captured deadlock information and will send the mail*/
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name ='DB Mail Profile',
@recipients='Deadlock@Gmail.com',
@copy_recipients='Perfteam@slk-soft.com',
@subject = @mysubject,
@body = 'Deadlock has occurred. View attachment to see the deadlock info',
/*Actual Query to get the deadlock information and send the information via mail*/
@query = 'SELECT B.* FROM TEMPDB.DBO.ERRORLOG B ,
(SELECT TOP 1 A.LOGDATE, A.PROCINFO FROM TEMPDB.DBO.ERRORLOG A
WHERE A.ERRORLOG LIKE ''%Deadlock%''
--AND CONVERT(VARCHAR,A.LOGDATE,112)=CONVERT(VARCHAR,GETUTCDATE(),112)
ORDER BY A.PROCINFO DESC
)A
WHERE B.LOGDATE=A.LOGDATE AND B.PROCINFO=A.PROCINFO ORDER BY 1',
@query_result_width = 600,
@attach_query_result_as_file = 1,
@importance='HIGH'

END
END
DROP TABLE tempdb.dbo.ErrorLog



Thursday, 23 February 2012

Backup database report in the server

Add the below script to step of backup job to get an alert

DECLARE @tableHTML  NVARCHAR(MAX) ;
SET @tableHTML =
    N'<H2>Last Backup Report For</H2>' +
    N'<H3>'+ (select td = (select @@servername)) +'</H3>'+
    N'<table border="1">' +
    N'<tr><th>Database Name</th><th>Days Since Last Backup</th>' +  
    N'<th>Last Backup Date</th>' +
    CAST ( ( SELECT td = msdb.dbo.backupset.database_name,       '',
                    td = datediff(day, max(msdb.dbo.backupset.backup_finish_date), getdate()), '',
                    td = CONVERT(VARCHAR(20), max(msdb.dbo.backupset.backup_finish_date), 100)
              FROM msdb.dbo.backupmediafamily
           INNER JOIN msdb.dbo.backupset
        ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
              WHERE msdb.dbo.backupset.database_name IN (SELECT name FROM master.sys.databases)
       GROUP BY msdb.dbo.backupset.database_name
       ORDER BY datediff(day, max(msdb.dbo.backupset.backup_finish_date), getdate()) DESC
              FOR XML PATH('tr'), TYPE
    ) AS NVARCHAR(MAX) ) +
N'</table>' ;
EXEC msdb.dbo.sp_send_dbmail
      @profile_name = 'Project',
    @recipients='Email Id',
    @subject = ' Production Server Backup Report',
    @body = @tableHTML,
    @body_format = 'HTML' ;

Tuesday, 17 May 2011

SSIS Documentation

Description



  • Author: Brian Knight, Allan Mitchell, Darren Green, and Douglas Hinson
  • Publisher: Wrox
  • Publish Date: January 12, 2006
  • Pages: 720

This book will help you get past the initial learning curve quickly so that you can get started using SSIS to transform data, create a workflow, or maintain your SQL Server. Offering you hands-on guidance, you'll learn a new world of integration possibilities and be able to move away from scripting complex logic to programming tasks using a full-featured language.
What you will learn from this book
  • Ways to quickly move and transform data
  • How to configure every aspect of SSIS
  • How to interface SSIS with web services and XML
  • Techniques to scale the SSIS and make it more reliable
  • How to migrate DTS packages to SSIS
  • How to create your own custom tasks and user interfaces
  • How to create an application that interfaces with SSIS to manage the environment
  • A detailed usable case study for a complete ETL solution
Who this book is for
This book is for developers, DBAs, and users who are looking to program custom code in all of the .NET languages. It is expected that you know the basics of how to query the SQL Server and have some fundamental programming skills.

Monday, 11 April 2011

When does my SQL Server support end ?

SQL Server VersionRelease DateMainstream Support EndExtended Support EndLink to Microsoft Support Lifecycle
SQL 703-01-199912-31-200501-11-2011Support Lifecycle
SQL 200011-30-200004-08-200804-09-2013Support Lifecycle
SQL 200501-14-200604-12-201104-12-2016Support Lifecycle
SQL 200811-06-200801-14-201401-08-2019Support Lifecycle
SQL 2008 R207-20-201001-14-201401-08-2019Support Lifecycle

The Support Lifecycle links will have more detailed information about Service Packs and support guidelines for the specific versions.

Query to find Service account of SQL Server

DECLARE @serviceaccount varchar(100)
EXECUTE master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE',
N'SYSTEM\CurrentControlSet\Services\MSSQLSERVER',
N'ObjectName',
@ServiceAccount OUTPUT,
N'no_output'
SELECT @Serviceaccount

How to retrieve table name and privilages of user in SQL server

Select TABLE_NAME, PRIVILEGE_TYPE from INFORMATION_SCHEMA.TABLE_PRIVILEGES where
GRANTEE = 'Toolbox'

Query to find the job history in SQL Server

select a.run_date,b.name,a.run_duration,run_time,a.server  from sysjobhistory a inner join sysjobs b on a.job_id=b.job_id where a.job_id='8E7D12F1-6B7E-4C08-8063-D8AAFD99F267' and a.step_id =0  order by a.run_date desc

Query to find the object permission for specific objects

select
sys.database_permissions.state_desc + ' ' + sys.database_permissions.permission_name + ' on ['+ sys.schemas.name + '].[' + sys.objects.name + '] to [' + sys.database_principals.name + ']' COLLATE LATIN1_General_CI_AS
from sys.database_permissions
join sys.objects on sys.database_permissions.major_id =
sys.objects.object_id
join sys.schemas on sys.objects.schema_id = sys.schemas.schema_id
join sys.database_principals on sys.database_permissions.grantee_principal_id =
sys.database_principals.principal_id
where sys.objects.name='object_name’

How to find table count of all tables in a database in sql

How to find table count of all tables in a database


SELECT o.name AS "Table Name", i.rowcnt AS "Row Count"

FROM sysobjects o, sysindexes i

WHERE i.id = o.id

AND indid IN(0,1)

AND xtype = 'u'

AND o.name <> 'sysdiagrams'

ORDER BY i.rowcnt DESC