Tag Archives: DBA Scripts

SQL Script to find the database & their files details on a SQL Server

By | March 30, 2018

SQL Script to find database details Sometimes we need a SQL Script which can give the below information about the databases created on a SQL Server for house keeping purpose: -1) Database Name 2) Database ID 3) Database Files name with their physical location 4) Database Creation Time 5) Users access type for Database 6)… Read More »

SQL Script: – How to find the database restoring history of SQL Server databases

By | March 30, 2018

SQL Script to find database restoring history Sometime we need to find out the history of database restoration to get the answers of following questions: -1) Do we need to restore the database again as there is a possibility that it is restored recently? 2) Which backup file (with location information) is used for database restoration?… Read More »

SQL Script: – How to find the last access date of a database

By | March 29, 2018

SQL Script to find database last access date Sometime we create databases on Test/ Development database servers for temporary uses for short duration and  forgot to delete them after the work got completed. As a part of house keeping activity, we need to find out when a database is last accessed in order to find… Read More »

SQL Script to find all the triggers defined on a database or on a single table

By | August 14, 2015

SQL Script to find triggers in SQL Database Sometime we need to find all the triggers defined on the database. So in this case, we can use the below SQL Query:-SELECT tbl.name as [Table Name] , trig.name as [Trigger Name] , trig.is_disabled FROM [sys].[triggers] as trig INNER JOIN sys.tables as tbl ON trig.parent_id = tbl.object_idIn… Read More »

Script to find complete months between two given dates

By | July 10, 2015

Complete months between two given dates Below script will help in finding the complete months within the 2 given dates. In the below script we takes below dates as exampleStartdate =2015-07-05 Enddate =2016-01-06 /**********************Script Start**************************/ IF (object_id(‘tempdb..#month’) is not null) DROP TABLE #month CREATE TABLE #month (id int identity(1,1),MonthStartDate DATETIME,MonthEndDate DATETIME) DECLARE @startdate AS DATETIME… Read More »

Script to find the complete weeks within two given dates

By | July 10, 2015

Complete weeks within two given dates Sometimes we need to find out the complete weeks within two given dates. Below script will help in finding the complete weeks within 2 given dates. In this script, I have used below dates as exampleStartdate=2015-04-12 Enddate=2015-06-10 SET DATEFORMAT YMD if (object_id(‘tempdb..#weekdays’) is not null) drop table #weekdays Declare… Read More »

SQL Script to find the missing indexes

By | January 25, 2015

Script to find the missing indexes Performance tuning in SQL is important exercise and index creation is an important part of it. Below script will help in finding the missing indexes. Once you create these indexes, it will help in improving the Performance.SELECT db_name(d.database_id) dbname , object_name(d.object_id) tablename , d.equality_columns , d.inequality_columns , d.included_columns ,’CREATE INDEX [missing_index_’ +… Read More »

SQL Script to find the list of all the jobs failed yesterday

By | January 20, 2015

One of the important task of any DBA is to find out all the jobs which are failed yesterday. Below SQL Script can be used to find out all the jobs which are failed yesterday. SELECT DISTINCT CAST(CONVERT(datetime,CAST(run_date AS char(8)),101) AS char(11)) AS ‘Failure Date’, SUBSTRING(T2.name,1,40) AS ‘Job Name’, T1.step_id AS ‘Step_id’, T1.step_name AS ‘Step… Read More »

SQL Script to search stored procedures containing a given text

By | January 20, 2015

Sometimes we need to find out how many stored procedures contains a given text. Below SQL query can be used to find out the list of all the stored procedures which contains a  particular given text as input. SELECT OBJECT_NAME(object_id), OBJECT_DEFINITION(object_id) FROM sys.procedures WHERE OBJECT_DEFINITION(object_id) LIKE ‘%Given text%’