Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Vivek's SQL Notes

Vivek johari is currently a Analyst and have more that 5.5 yeras of experience in database. He has Master degree in Computer and also he is Microsoft certified Sql DBA (MCTS)& Microsoft certified SQl BI professional(MCTS). He is also Oracle certified profession(OCP)DBA in ORACLE 10g and ORACLE 9i.He has the experience of working in PL/SQL, T-SQL and SSIS/SSRS. His work basically involved designing and optimization of the Database.He has also published many database articles on his blog Technologies with Vivek Johari.

Happy Independence Day - Jai Hind

Wishing you a very

Happy Independence Day


" We owe a lot to the Indians, who taught us how to count, without which no worthwhile scientific discovery could have been made. "Albert Einstein.



     Some facts about India 
  1. Indus Valley Civilization is the world’s oldest civilization.So, India is…

Read more

0 comments, 143 reads

Posted in Vivek's SQL Notes on 15 August 2015

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

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…

Read more

0 comments, 139 reads

Posted in Vivek's SQL Notes on 14 August 2015

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

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…

Read more

0 comments, 186 reads

Posted in Vivek's SQL Notes on 13 August 2015

Question of the Month (July)

Suppose we have a table emp_plan  which contains 4 columns "Empid" (employeeid), Planid (Projectid), Startdate(Allocation Start date) and Enddate (Allocation Enddate). Its structure is given below:-


Create table emp_plan (empid nvarchar(200),planid nvarchar(20),startdate datetime, enddate datetime)

Also below is the script to enter sample data into the table:-

insert into emp_plan(empid,planid,startdate,enddate)
select…

Read more

4 comments, 107 reads

Posted in Vivek's SQL Notes on 3 August 2015

Do you know about Whatsapp web?


Whatsapp claims that they currently have 500 million users worldwide. That is pretty impressive looking at the good amount of distinctively decent competitors they have within the space of Social Networking communication. ViberLineGoogle Hangout,Facebook Messenger & Hike (Commonly in India). While all these products…

Read more

0 comments, 162 reads

Posted in Vivek's SQL Notes on 11 July 2015

Script to find 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 example

Startdate =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

0 comments, 179 reads

Posted in Vivek's SQL Notes on 10 July 2015

Script to find the 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 example 

Startdate=2015-04-12
Enddate=2015-06-10 

SET DATEFORMAT YMD
if (object_id('tempdb..#weekdays') is not null)
drop table #weekdays

Declare…

Read more

0 comments, 150 reads

Posted in Vivek's SQL Notes on 10 July 2015

Importance of 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 latter on how best practices can be implemented in our database code.

Code written…

Read more

0 comments, 1,000 reads

Posted in Vivek's SQL Notes on 1 February 2015

SQL 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

1 comments, 414 reads

Posted in Vivek's SQL Notes on 25 January 2015

SQL SERVER - Data types

In relational database, we store data in tabular form where data is divided into columns. Each column has a name and a data type which shows what type of data is going to store in that column. Below are the data types which are mostly used in SQL server. 

CHARACTER(n)   

Read more

0 comments, 233 reads

Posted in Vivek's SQL Notes on 25 January 2015

SQL Server - Convert() function

Convert () function in SQL Server to convert an expression from one data type to another data type

Syntax for CONVERT function
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

Convert functions is usually used with date/time data type to convert a date in different formats. Below examples…

Read more

0 comments, 363 reads

Posted in Vivek's SQL Notes on 24 January 2015

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

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…

Read more

0 comments, 1,720 reads

Posted in Vivek's SQL Notes on 20 January 2015

SQL Script to search stored procedures containing a given text

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%'

Read more

1 comments, 213 reads

Posted in Vivek's SQL Notes on 20 January 2015

SQL Script to find the tables created from a given date

Below is the SQL query which can be used to find out the tables which are created or modified on or from a given date.

SELECT
        [name] as Tablename
       ,create_date
       ,modify_date
FROM
        sys.tables where cast(create_date as…

Read more

0 comments, 268 reads

Posted in Vivek's SQL Notes on 20 January 2015

SQL Script to find the databases size

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,
 …

Read more

0 comments, 231 reads

Posted in Vivek's SQL Notes on 20 January 2015

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

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…

Read more

0 comments, 461 reads

Posted in Vivek's SQL Notes on 9 January 2015

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

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)…

Read more

0 comments, 372 reads

Posted in Vivek's SQL Notes on 9 January 2015

SQL Script to find the last executed commands on the SQl Server

Sometimes we needs to find out the last executed commands on the database server. 
Below is the SQL script share by one of my friend Ken Watson to find out the last executed queries.

SELECT usecounts, cacheobjtype, objtype, text 
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
WHERE usecounts > 1 
ORDER BY…

Read more

1 comments, 337 reads

Posted in Vivek's SQL Notes on 8 January 2015

Script to find the Fragmentation of indexes

Below is the script to find the fragmentation of the indexes created on a database.

SELECT OBJECT_NAME(OBJECT_ID), index_id,index_type_desc,index_level,
avg_fragmentation_in_percent,avg_page_space_used_in_percent,page_count
FROM sys.dm_db_index_physical_stats (DB_ID( N'Database name') , NULL, NULL, NULL , 'SAMPLED') ORDER BY avg_fragmentation_in_percent DESC

avg_fragmentation_in_percent represents  logical fragmentation.

If this value is higher than 5% and less than 30%,…

Read more

3 comments, 6,079 reads

Posted in Vivek's SQL Notes on 4 January 2015

Script to find all the running queries/jobs on the Database Server

Below is the query to find out all the Queries and their details like SSID etc. running on the SQL Server from all databases. It helps in finding out which queries/jobs running on the server. It can be helpful in detecting the blocking queries also.


USE Master
SET NOCOUNT ON…

Read more

0 comments, 533 reads

Posted in Vivek's SQL Notes on 4 January 2015

Older posts