Category Archives: SQL Advanced

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 »

Interview question – Is Clustered index on column with duplicate values possible?

By | August 13, 2015

Through this article, we are going to discuss three important interview questions of SQL which are given below:-  1) Can we create clustered index on a column containing duplicate values?  2) Can we create a Primary Key on a table on which a clustered index is already defined? 3) If a clustered index is already defined on a… Read More »

Importance of Best Practices in database programming

By | February 1, 2015

Best Practices in database programming For any programming language, just writing the code is not well enough. It should be written using the best practices. This article will try to explain the disadvantages of writing code without using Best Practices and later on how best practices can be implemented in our database code. Code written… 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. Sometimes base on the frequent SQL queries, we need to create some indexes or missing indexes which are enhance the query performance. Below script will help in finding the missing indexes. SELECT db_name(d.database_id) dbname , object_name(d.object_id)… 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%’

SQL Script to find the databases size

By | January 20, 2015

Sometimes we need to find out the size of the database on a server. Below SQL Scripts can be used to find out the size of all the databases created on the server.  ;WITH DBSize (SqlServerInstanceName, DatabaseName, DatabaseSize, DBLogSize, TotalDBSize) AS (   SELECT      @@SERVERNAME SqlServerInstanceName,             db.name AS… Read More »

T-SQL script to find the growth size of database files

By | January 9, 2015

Below query can be used to see the growth size of database files. DECLARE @filename NVARCHAR(1000); DECLARE @bc INT; DECLARE @ec INT; DECLARE @bfn VARCHAR(1000); DECLARE @efn VARCHAR(10); — Get the name of the current default trace SELECT @filename = CAST(value AS NVARCHAR(1000)) FROM ::fn_trace_getinfo(DEFAULT) WHERE traceid = 1 AND property = 2; — rip… Read More »

T-SQL script to find the highly populated tables in database

By | January 9, 2015

Sometimes we need to check which tables of our database is highly populated. Below is the T-SQL Script which we can use to find out that. SELECT      t.NAME AS TableName,     i.name as indexName,     sum(p.rows) as RowCounts,     sum(a.total_pages) as TotalPages,      sum(a.used_pages) as UsedPages,      sum(a.data_pages)… Read More »