<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Script &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/category/script/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Tue, 21 Nov 2017 06:18:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>Find top N records for each group in MySQL</title>
		<link>https://www.dbrnd.com/2015/08/find-top-n-records-for-each-group-in-mysql/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 30 Aug 2015 15:44:45 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL DBA Script]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[FIND_IN_SET]]></category>
		<category><![CDATA[GROUP_CONCAT]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=464</guid>

					<description><![CDATA[In this post, I am sharing a demonstration on finding top N records for each group in MySQL. Today morning, I was working on one of the report and requirement was to find top 2 employee working hours for each department. I tried many scripts and finally got the solution. Create a table with sample [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&#038;title=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" data-a2a-url="https://www.dbrnd.com/2015/08/find-top-n-records-for-each-group-in-mysql/" data-a2a-title="Find top N records for each group in MySQL"></a></p><p>In this post, I am sharing a demonstration on finding top N records for each group in MySQL.</p>
<p>Today morning, I was working on one of the report and requirement was to find top 2 employee working hours for each department.</p>
<p>I tried many scripts and finally got the solution.</p>
<p><strong>Create a table with sample data:</strong></p><pre class="crayon-plain-tag">CREATE DATABASE Employee;

CREATE TABLE Employee.EmployeePivotTest
(
     EmpName VARCHAR(255)
    ,EmpDeptName VARCHAR(255)
    ,EmpAvgWorkingHours INTEGER
);

INSERT INTO Employee.EmployeePivotTest VALUES
('Anvesh','Computer-IT',226)
,('Anvesh','Computer-IT',100)
,('Anvesh','Computer-IT',200)
,('Anvesh','Computer-IT',752)
,('Anvesh','Account',142)
,('Anvesh','Marketing',110)
,('Anvesh','Finance',236)
,('Anvesh','Account',120)
,('Neevan','Computer-IT',120)
,('Neevan','Finance',852)
,('Neevan','Account',326)
,('Neevan','Marketing',50)
,('Neevan','Finance',140);</pre><p>
<p><strong>Find a top N working hour by each employee:</strong></p><pre class="crayon-plain-tag">SELECT 
    EP.EmpName
    ,EmpDeptName
    ,EmpAvgWorkingHours
FROM Employee.EmployeePivotTest  AS EP
INNER JOIN
(
   SELECT 
	EmpName
	,GROUP_CONCAT(DISTINCT EmpAvgWorkingHours 
         ORDER BY EmpAvgWorkingHours DESC) 
         AS GroupedWorkingHours
   FROM Employee.EmployeePivotTest 
   GROUP BY EmpName
) AS T
ON EP.EmpName = T.EmpName 
AND FIND_IN_SET(EmpAvgWorkingHours,GroupedWorkingHours) &lt;=2
ORDER BY EmpName,EmpDeptName;</pre><p>
<p><strong>Result:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLTopN.png"><img decoding="async" class="alignnone wp-image-468 size-full" title="MySQL Top N Record for Each Group" src="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLTopN.png" alt="MySQL Top N Record for Each Group" width="308" height="117" srcset="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLTopN.png 308w, https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLTopN-300x114.png 300w, https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLTopN-150x57.png 150w" sizes="(max-width: 308px) 100vw, 308px" /></a></p>
<p>Now check the above result, each employee has two top 2 records based on their working hours.</p>
<blockquote><p>In the above query, I used GROUP_CONCAT and FIND_IN_SET.<br />
GROUP_CONCAT groups the column value into a single string and FIND_IN_SET is used to get the position of the string in a comma delimited string list.</p></blockquote>
<p>You can change the value of 2 to N for your customized report.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Find Table Dependency in MySQL</title>
		<link>https://www.dbrnd.com/2015/08/find-table-dependency-in-constraint-view-stored-procedure-of-mysql/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 15 Aug 2015 06:18:00 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL DBA Script]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<category><![CDATA[Object Dependency]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=381</guid>

					<description><![CDATA[In this post, I am sharing different scripts to find table dependency for Foreign Key Constraint, View and Stored Procedure of MySQL. Database Architecture or Database Developer has to find table dependency before any DDL changes on the Tables. Below are three different scripts: [crayon-6ab604e8af7a8755576801/] [crayon-6ab604e8af7ab220157165/] [crayon-6ab604e8af7ac004525553/]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-table-dependency-in-constraint-view-stored-procedure-of-mysql%2F&amp;linkname=Find%20Table%20Dependency%20in%20MySQL" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-table-dependency-in-constraint-view-stored-procedure-of-mysql%2F&amp;linkname=Find%20Table%20Dependency%20in%20MySQL" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-table-dependency-in-constraint-view-stored-procedure-of-mysql%2F&amp;linkname=Find%20Table%20Dependency%20in%20MySQL" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-table-dependency-in-constraint-view-stored-procedure-of-mysql%2F&amp;linkname=Find%20Table%20Dependency%20in%20MySQL" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-table-dependency-in-constraint-view-stored-procedure-of-mysql%2F&amp;linkname=Find%20Table%20Dependency%20in%20MySQL" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-table-dependency-in-constraint-view-stored-procedure-of-mysql%2F&amp;linkname=Find%20Table%20Dependency%20in%20MySQL" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-table-dependency-in-constraint-view-stored-procedure-of-mysql%2F&#038;title=Find%20Table%20Dependency%20in%20MySQL" data-a2a-url="https://www.dbrnd.com/2015/08/find-table-dependency-in-constraint-view-stored-procedure-of-mysql/" data-a2a-title="Find Table Dependency in MySQL"></a></p><p>In this post, I am sharing different scripts to find table dependency for Foreign Key<br />
Constraint, View and Stored Procedure of MySQL.</p>
<p>Database Architecture or Database Developer has to find table dependency before any DDL changes on the Tables.</p>
<p><strong>Below are three different scripts: </strong></p>
<pre class="crayon-plain-tag">/*Find Table Dependency in Foreign Key Constraint.*/
SELECT 
     Constraint_Type
    ,Constraint_Name
    ,Table_Schema
    ,Table_Name
FROM information_schema.table_constraints
WHERE Constraint_Type = 'FOREIGN KEY'
    AND Table_Name = 'Any_Table'
    AND Table_Schema ='db_Name';</pre> </p>
<pre class="crayon-plain-tag">/*Find Table Dependency in Views.*/
SELECT *
FROM information_schema.views 
WHERE Table_Schema='db_name' 
  AND Table_Name = 'table_name'</pre> </p>
<pre class="crayon-plain-tag">/*Find Table Dependency in Stored Procedure.*/
SELECT 
    Table_Schema
    ,Table_Name
    ,Table_Type
    ,Engine
    ,Routine_Name
    ,Routine_Schema
    ,Routine_Type
FROM information_schema.tables
 INNER JOIN information_schema.routines
    ON routines.routine_definition 
    LIKE  Concat('%', tables.table_name, '%')
WHERE Table_Schema ='db_name'</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Script to find last DDL changes in SQL Server</title>
		<link>https://www.dbrnd.com/2015/06/script-to-find-last-ddl-changes-in-sql-server/</link>
					<comments>https://www.dbrnd.com/2015/06/script-to-find-last-ddl-changes-in-sql-server/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 28 Jun 2015 06:49:23 +0000</pubDate>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[sys.fn_trace_getinfo]]></category>
		<category><![CDATA[sys.fn_trace_gettable]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=291</guid>

					<description><![CDATA[In this post, I am sharing a script to find who last performed the DDL changes in SQL Server. As a DBA, this script is handy to monitor recent DDL changes in SQL Server. Below is a script: [crayon-6ab604e8b01df660920933/] In the above script, sys.fn_trace_getinfo(NULL) will fetch default trace file.]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-to-find-last-ddl-changes-in-sql-server%2F&amp;linkname=Script%20to%20find%20last%20DDL%20changes%20in%20SQL%20Server" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-to-find-last-ddl-changes-in-sql-server%2F&amp;linkname=Script%20to%20find%20last%20DDL%20changes%20in%20SQL%20Server" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-to-find-last-ddl-changes-in-sql-server%2F&amp;linkname=Script%20to%20find%20last%20DDL%20changes%20in%20SQL%20Server" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-to-find-last-ddl-changes-in-sql-server%2F&amp;linkname=Script%20to%20find%20last%20DDL%20changes%20in%20SQL%20Server" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-to-find-last-ddl-changes-in-sql-server%2F&amp;linkname=Script%20to%20find%20last%20DDL%20changes%20in%20SQL%20Server" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-to-find-last-ddl-changes-in-sql-server%2F&amp;linkname=Script%20to%20find%20last%20DDL%20changes%20in%20SQL%20Server" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-to-find-last-ddl-changes-in-sql-server%2F&#038;title=Script%20to%20find%20last%20DDL%20changes%20in%20SQL%20Server" data-a2a-url="https://www.dbrnd.com/2015/06/script-to-find-last-ddl-changes-in-sql-server/" data-a2a-title="Script to find last DDL changes in SQL Server"></a></p><p>In this post, I am sharing a script to find who last performed the DDL changes in SQL Server.</p>
<p>As a DBA, this script is handy to monitor recent DDL changes in SQL Server.</p>
<p><strong>Below is a script:</strong></p><pre class="crayon-plain-tag">SELECT 
	te.name AS eventtype
	,t.loginname
	,t.spid
	,t.starttime
	,t.objectname
	,t.databasename
	,t.hostname
	,t.ntusername
	,t.ntdomainname
	,t.clientprocessid
	,t.applicationname	
FROM sys.fn_trace_gettable
(
	CONVERT
	(VARCHAR(150)
	,(
		SELECT TOP 1 
			value
		FROM sys.fn_trace_getinfo(NULL)  
		WHERE property = 2
	)),DEFAULT
) T 
INNER JOIN sys.trace_events as te 
	ON t.eventclass = te.trace_event_id 
WHERE eventclass=164</pre><p>
<p>In the above script, sys.fn_trace_getinfo(NULL) will fetch default trace file.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/06/script-to-find-last-ddl-changes-in-sql-server/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>Script to Find Slowest Running Query in SQL Server</title>
		<link>https://www.dbrnd.com/2015/06/script-find-slowest-running-query-in-sql-server/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 21 Jun 2015 10:46:40 +0000</pubDate>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dm_exec_query_plan]]></category>
		<category><![CDATA[dm_exec_query_stats]]></category>
		<category><![CDATA[slowest query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=271</guid>

					<description><![CDATA[In this post, I am sharing a script to find slowest running queries in SQL Server. Below is a SQL Query: [crayon-6ab604e8b06b7363989167/] Using above query you can find top 10 slowest queries with query plan text.]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-find-slowest-running-query-in-sql-server%2F&amp;linkname=Script%20to%20Find%20Slowest%20Running%20Query%20in%20SQL%20Server" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-find-slowest-running-query-in-sql-server%2F&amp;linkname=Script%20to%20Find%20Slowest%20Running%20Query%20in%20SQL%20Server" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-find-slowest-running-query-in-sql-server%2F&amp;linkname=Script%20to%20Find%20Slowest%20Running%20Query%20in%20SQL%20Server" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-find-slowest-running-query-in-sql-server%2F&amp;linkname=Script%20to%20Find%20Slowest%20Running%20Query%20in%20SQL%20Server" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-find-slowest-running-query-in-sql-server%2F&amp;linkname=Script%20to%20Find%20Slowest%20Running%20Query%20in%20SQL%20Server" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-find-slowest-running-query-in-sql-server%2F&amp;linkname=Script%20to%20Find%20Slowest%20Running%20Query%20in%20SQL%20Server" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fscript-find-slowest-running-query-in-sql-server%2F&#038;title=Script%20to%20Find%20Slowest%20Running%20Query%20in%20SQL%20Server" data-a2a-url="https://www.dbrnd.com/2015/06/script-find-slowest-running-query-in-sql-server/" data-a2a-title="Script to Find Slowest Running Query in SQL Server"></a></p><p>In this post, I am sharing a script to find slowest running queries in SQL Server.</p>
<p><strong>Below is a SQL Query:</strong></p>
<pre class="crayon-plain-tag">SELECT TOP 10 
	SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1,
	((CASE qs.statement_end_offset WHEN -1 THEN DATALENGTH(qt.TEXT)
	ELSE qs.statement_end_offset END - qs.statement_start_offset)/2)+1
	) AS Query
	,qp.query_plan
	,qs.execution_count
	,qs.total_logical_reads
	,qs.last_logical_reads
	,qs.total_logical_writes
	,qs.last_logical_writes
	,qs.total_worker_time
	,qs.last_worker_time
	,qs.total_elapsed_time
	,qs.last_elapsed_time
	,qs.last_execution_time	
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
ORDER BY qs.total_logical_reads DESC</pre>
Using above query you can find top 10 slowest queries with query plan text.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Script to find Cross Database Object Dependency in SQL Server</title>
		<link>https://www.dbrnd.com/2015/06/sql-script-to-find-cross-database-object-dependency-in-sql-server/</link>
					<comments>https://www.dbrnd.com/2015/06/sql-script-to-find-cross-database-object-dependency-in-sql-server/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 20 Jun 2015 12:35:06 +0000</pubDate>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Cross Database]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[Object Dependency]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=267</guid>

					<description><![CDATA[In the previous post, I Have provided three different scripts to find Object Dependency in the current database. In this post I share one SQL script to find Cross Database Object Dependency in SQL Server. [crayon-6ab604e8b08db798881682/]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-cross-database-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Cross%20Database%20Object%20Dependency%20in%20SQL%20Server" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-cross-database-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Cross%20Database%20Object%20Dependency%20in%20SQL%20Server" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-cross-database-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Cross%20Database%20Object%20Dependency%20in%20SQL%20Server" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-cross-database-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Cross%20Database%20Object%20Dependency%20in%20SQL%20Server" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-cross-database-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Cross%20Database%20Object%20Dependency%20in%20SQL%20Server" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-cross-database-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Cross%20Database%20Object%20Dependency%20in%20SQL%20Server" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-cross-database-object-dependency-in-sql-server%2F&#038;title=SQL%20Script%20to%20find%20Cross%20Database%20Object%20Dependency%20in%20SQL%20Server" data-a2a-url="https://www.dbrnd.com/2015/06/sql-script-to-find-cross-database-object-dependency-in-sql-server/" data-a2a-title="SQL Script to find Cross Database Object Dependency in SQL Server"></a></p><p>In the previous post, I Have provided three different scripts to find Object Dependency in the current database.</p>
<p>In this post I share one SQL script to find Cross Database Object Dependency in SQL Server.</p>
<pre class="crayon-plain-tag">SELECT  
	OBJECT_NAME (referencing_id) AS ReferencedObjects
	,referenced_database_name
	,referenced_schema_name
	,referenced_entity_name
FROM sys.sql_expression_dependencies
WHERE referenced_database_name IS NOT NULL
AND is_ambiguous = 0;</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/06/sql-script-to-find-cross-database-object-dependency-in-sql-server/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Script to find Object Dependency in SQL Server</title>
		<link>https://www.dbrnd.com/2015/06/sql-script-to-find-object-dependency-in-sql-server/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 20 Jun 2015 12:26:30 +0000</pubDate>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[Object Dependency]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=260</guid>

					<description><![CDATA[As a DBA or Database Developer, we need to find the object dependency into the current database. Finding the Object Dependency is very useful before any alteration on object like ALTER, DROP. Below are three reference scripts to find the object dependency. [crayon-6ab604e8b0aaf856157712/] [crayon-6ab604e8b0ab0483192431/] [crayon-6ab604e8b0ab1389500331/]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Object%20Dependency%20in%20SQL%20Server" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Object%20Dependency%20in%20SQL%20Server" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Object%20Dependency%20in%20SQL%20Server" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Object%20Dependency%20in%20SQL%20Server" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Object%20Dependency%20in%20SQL%20Server" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-object-dependency-in-sql-server%2F&amp;linkname=SQL%20Script%20to%20find%20Object%20Dependency%20in%20SQL%20Server" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fsql-script-to-find-object-dependency-in-sql-server%2F&#038;title=SQL%20Script%20to%20find%20Object%20Dependency%20in%20SQL%20Server" data-a2a-url="https://www.dbrnd.com/2015/06/sql-script-to-find-object-dependency-in-sql-server/" data-a2a-title="SQL Script to find Object Dependency in SQL Server"></a></p><p>As a DBA or Database Developer, we need to find the object dependency into the current database.</p>
<blockquote><p>Finding the Object Dependency is very useful before any alteration on object like ALTER, DROP.</p></blockquote>
<p>Below are three reference scripts to find the object dependency. </p>
<pre class="crayon-plain-tag">SELECT OBJECT_NAME(OBJECT_ID)
  FROM sys.sql_modules
 WHERE DEFINITION LIKE '%tbl_Employee%'
GO</pre>
<pre class="crayon-plain-tag">SELECT DISTINCT 
	SO.Name
	,SO.XType AS ObjectType
	,SC.TEXT AS ObjectDefinition
FROM syscomments AS SC
INNER JOIN sysobjects AS SO ON SC.id=SO.id
WHERE SC.TEXT LIKE '%tablename%'
GO</pre>
<pre class="crayon-plain-tag">SELECT 
    referencing_schema_name, referencing_entity_name, referencing_id, 
    referencing_class_desc, is_caller_dependent
FROM 
    sys.dm_sql_referencing_entities ('SchemaName.TableName', 'Object');
GO</pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to find any Text, Column, Comment, Object Name in SQL Server Objects</title>
		<link>https://www.dbrnd.com/2015/06/how-to-find-any-text-column-comment-object-name-in-sql-server-objects/</link>
					<comments>https://www.dbrnd.com/2015/06/how-to-find-any-text-column-comment-object-name-in-sql-server-objects/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 20 Jun 2015 11:44:20 +0000</pubDate>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Objects]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=253</guid>

					<description><![CDATA[In this post, I am sharing a script to find any text, column name, comment and object name from the SQL Server. Below are the different scripts: [crayon-6ab604e8b0b21395798250/] [crayon-6ab604e8b0b22117596385/] [crayon-6ab604e8b0b23387238945/]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fhow-to-find-any-text-column-comment-object-name-in-sql-server-objects%2F&amp;linkname=How%20to%20find%20any%20Text%2C%20Column%2C%20Comment%2C%20Object%20Name%20in%20SQL%20Server%20Objects" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fhow-to-find-any-text-column-comment-object-name-in-sql-server-objects%2F&amp;linkname=How%20to%20find%20any%20Text%2C%20Column%2C%20Comment%2C%20Object%20Name%20in%20SQL%20Server%20Objects" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fhow-to-find-any-text-column-comment-object-name-in-sql-server-objects%2F&amp;linkname=How%20to%20find%20any%20Text%2C%20Column%2C%20Comment%2C%20Object%20Name%20in%20SQL%20Server%20Objects" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fhow-to-find-any-text-column-comment-object-name-in-sql-server-objects%2F&amp;linkname=How%20to%20find%20any%20Text%2C%20Column%2C%20Comment%2C%20Object%20Name%20in%20SQL%20Server%20Objects" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fhow-to-find-any-text-column-comment-object-name-in-sql-server-objects%2F&amp;linkname=How%20to%20find%20any%20Text%2C%20Column%2C%20Comment%2C%20Object%20Name%20in%20SQL%20Server%20Objects" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fhow-to-find-any-text-column-comment-object-name-in-sql-server-objects%2F&amp;linkname=How%20to%20find%20any%20Text%2C%20Column%2C%20Comment%2C%20Object%20Name%20in%20SQL%20Server%20Objects" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fhow-to-find-any-text-column-comment-object-name-in-sql-server-objects%2F&#038;title=How%20to%20find%20any%20Text%2C%20Column%2C%20Comment%2C%20Object%20Name%20in%20SQL%20Server%20Objects" data-a2a-url="https://www.dbrnd.com/2015/06/how-to-find-any-text-column-comment-object-name-in-sql-server-objects/" data-a2a-title="How to find any Text, Column, Comment, Object Name in SQL Server Objects"></a></p><p>In this post, I am sharing a script to find any text, column name, comment and object name from the SQL Server. </p>
<p><strong>Below are the different scripts:</strong></p>
<pre class="crayon-plain-tag">/*Find any text or object in SQL Server Objects*/
SELECT *FROM sys.objects 
WHERE OBJECT_DEFINITION (OBJECT_ID) LIKE '%tbl_Employee%'</pre>
<pre class="crayon-plain-tag">/*Find any text or object in SQL Server Comments*/
SELECT DISTINCT sysob.name
FROM syscomments AS sysco
INNER JOIN sysobjects AS sysob ON sysco.id=sysob.id
WHERE sysco.TEXT LIKE '%Order%'</pre>
<pre class="crayon-plain-tag">/*Find any column in database*/
SELECT 
	TABLE_SCHEMA
	,TABLE_NAME
	,COLUMN_NAME
	,DATA_TYPE
	,COLUMN_DEFAULT 		
FROM INFORMATION_SCHEMA.columns
WHERE COLUMN_NAME LIKE '%&lt;Your Column Name&gt;%'</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/06/how-to-find-any-text-column-comment-object-name-in-sql-server-objects/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>Query to Find Missing Number and ID in SQL Server</title>
		<link>https://www.dbrnd.com/2015/06/query-to-find-missing-number-id-sql-server/</link>
					<comments>https://www.dbrnd.com/2015/06/query-to-find-missing-number-id-sql-server/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 20 Jun 2015 10:36:29 +0000</pubDate>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[Missing]]></category>
		<category><![CDATA[Number]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=242</guid>

					<description><![CDATA[In this post, I will give you one full demo to find a missing number of identity column in SQL Server. Before a few days ago, I was working on production report and found that some number is missing. This sequence and missing number are very important. Immediately one requirement come to my desk is, [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&#038;title=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" data-a2a-url="https://www.dbrnd.com/2015/06/query-to-find-missing-number-id-sql-server/" data-a2a-title="Query to Find Missing Number and ID in SQL Server"></a></p><p>In this post, I will give you one full demo to find a missing number of identity column in SQL Server.</p>
<blockquote><p>Before a few days ago, I was working on production report and found that some number is missing. This sequence and missing number are very important.<br />
Immediately one requirement come to my desk is, please find a list of all missing number for investigation purpose.</p></blockquote>
<p>Below is a full demo script to find the missing number in SQL Server.</p>
<p>First Create one sample table with data.</p><pre class="crayon-plain-tag">/*Create one table to store Numbers*/

CREATE TABLE dbo.tbl_Numbers
(
	ID INTEGER
)
GO

/*Insert some test records. You can also see I have missed many number which we are going to find by query*/

INSERT INTO dbo.tbl_Numbers 
VALUES(1),(2),(3),(4),(6),(7),(9),(10)
,(11),(15),(16),(19),(22),(23),(24)
,(28),(32),(33),(34),(35),(38),(40)
,(41),(42),(43),(44),(45),(47),(49)
,(50)
GO</pre><p>
Below is step by step script to find this missing number.</p><pre class="crayon-plain-tag">/*Step 1: Create one temp table and store all distinct Numbers*/
SELECT DISTINCT ID INTO #DistinctID FROM dbo.tbl_Numbers
GO

/*Step 2: Find NextID which has something missing before it.
Store into one TempTable*/

SELECT 
	(ROW_NUMBER()OVER (ORDER BY ID ASC)) AS TableCountID
	,b.ID 
	,(
		SELECT Top 1 ID
		FROM #DistinctID a WITH (NOLOCK)
		WHERE a.ID &gt; b.ID
	) AS NextID
INTO #NextID
FROM #DistinctID AS b WITH (NOLOCK)
WHERE 
(
	SELECT Top 1 ID
	FROM #DistinctID a WITH (NOLOCK)
	WHERE a.ID &gt; b.ID
) &lt;&gt; b.ID+1
ORDER BY b.ID

/*Step 3: Create one temp table to store missing number details */

CREATE TABLE #MissingID
(
	TableCountID INTEGER
	,ID INTEGER
	,NextID INTEGER
	,MissingID INTEGER
	,TotalDiff INTEGER
)
GO

/*Step 4: Now start to find missing id using WHILE Loop. */

DECLARE @CountID INTEGER
DECLARE @InnerLoopCountID INTEGER
SET @CountID = 1
WHILE (@CountID &amp;gt; 0)
BEGIN
INSERT INTO #MissingID
SELECT 
	TableCountID
	,ID 
	,NextID
	,CASE WHEN TotalDiff = 1 THEN ID+TotalDiff END AS MissingID
	,TotalDiff		
FROM
(
SELECT	
	TableCountID
	,ID
	,NextID
	,(NextID-ID)-1 AS TotalDiff
FROM #NextID
) AS T WHERE TableCountID = @CountID

IF ((SELECT (NextID-ID)-1 FROM #NextID WHERE TableCountID = @CountID)&amp;gt;1)
BEGIN	
	SET @InnerLoopCountID = (SELECT (NextID-ID)-1 FROM #NextID WHERE TableCountID = @CountID)
	
WHILE (@InnerLoopCountID &amp;gt; 0)
BEGIN
	INSERT INTO #MissingID
	SELECT 
		TableCountID
		,ID 
		,NextID
		,ID+@InnerLoopCountID AS MissingNumber
		,TotalDiff		
	FROM
	(
	SELECT	
		TableCountID
		,ID
		,NextID
		,(NextID-ID)-1 AS TotalDiff
	FROM #NextID
	) AS T WHERE TableCountID = @CountID		

	SET @InnerLoopCountID = @InnerLoopCountID - 1				
END			
END
SET @CountID = @CountID+1

IF @CountID = (SELECT COUNT(1)+1 FROM #NextID)
BEGIN	
	RETURN
END
END
GO

/*Step 5: Now select missing data from #MissingID temp table. 
You can find full details on MissingID.*/

SELECT *FROM #MissingID WHERE MissingID IS NOT NULL ORDER BY MissingID 
GO</pre><p>
Result of Step 5:<a href="https://www.dbrnd.com/wp-content/uploads/2015/06/MissingID.png"><img fetchpriority="high" decoding="async" class="aligncenter wp-image-250 size-full" title="Missing number in sql server" src="https://www.dbrnd.com/wp-content/uploads/2015/06/MissingID.png" alt="Missing number in sql server" width="327" height="440" srcset="https://www.dbrnd.com/wp-content/uploads/2015/06/MissingID.png 327w, https://www.dbrnd.com/wp-content/uploads/2015/06/MissingID-223x300.png 223w, https://www.dbrnd.com/wp-content/uploads/2015/06/MissingID-150x202.png 150w" sizes="(max-width: 327px) 100vw, 327px" /></a>In the above result you can find total 20 missing number. MissingID Column represents missing numbers.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/06/query-to-find-missing-number-id-sql-server/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Insert script for all Countries</title>
		<link>https://www.dbrnd.com/2015/05/insert-script-for-all-countries/</link>
					<comments>https://www.dbrnd.com/2015/05/insert-script-for-all-countries/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 19 May 2015 10:00:46 +0000</pubDate>
				<category><![CDATA[MySQL DBA Script]]></category>
		<category><![CDATA[PostgreSQL DBA Script]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[countries]]></category>
		<category><![CDATA[Country table]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[SQL Server]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=110</guid>

					<description><![CDATA[Many times we require to insert all countries into a database system. In this post, I am sharing the Country data with required insert statements. We can store this data into Country table. The application has to just select list of Countries from this country table. Below is a sample Country table and insert script [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Finsert-script-for-all-countries%2F&amp;linkname=Insert%20script%20for%20all%20Countries" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Finsert-script-for-all-countries%2F&amp;linkname=Insert%20script%20for%20all%20Countries" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Finsert-script-for-all-countries%2F&amp;linkname=Insert%20script%20for%20all%20Countries" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Finsert-script-for-all-countries%2F&amp;linkname=Insert%20script%20for%20all%20Countries" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Finsert-script-for-all-countries%2F&amp;linkname=Insert%20script%20for%20all%20Countries" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Finsert-script-for-all-countries%2F&amp;linkname=Insert%20script%20for%20all%20Countries" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Finsert-script-for-all-countries%2F&#038;title=Insert%20script%20for%20all%20Countries" data-a2a-url="https://www.dbrnd.com/2015/05/insert-script-for-all-countries/" data-a2a-title="Insert script for all Countries"></a></p><p>Many times we require to insert all countries into a database system. In this post, I am sharing the Country data with required insert statements. </p>
<p>We can store this data into Country table. The application has to just select list of Countries from this country table.</p>
<p>Below is a sample Country table and  insert script for all Countries:</p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Countries
(
     CountryID INT
    ,CountryName VARCHAR(50)
    ,TwoCharCountryCode CHAR(2)
    ,ThreeCharCountryCode CHAR(3)    
);


INSERT INTO tbl_Countries VALUES 
(1,'Afghanistan','AF','AFG')
,(2,'Aland Islands','AX','ALA')
,(3,'Albania','AL','ALB')
,(4,'Algeria','DZ','DZA')
,(5,'American Samoa','AS','ASM')
,(6,'Andorra','AD','AND')
,(7,'Angola','AO','AGO')
,(8,'Anguilla','AI','AIA')
,(9,'Antarctica','AQ','ATA')
,(10,'Antigua and Barbuda','AG','ATG')
,(11,'Argentina','AR','ARG')
,(12,'Armenia','AM','ARM')
,(13,'Aruba','AW','ABW')
,(14,'Australia','AU','AUS')
,(15,'Austria','AT','AUT')
,(16,'Azerbaijan','AZ','AZE')
,(17,'Bahamas','BS','BHS')
,(18,'Bahrain','BH','BHR')
,(19,'Bangladesh','BD','BGD')
,(20,'Barbados','BB','BRB')
,(21,'Belarus','BY','BLR')
,(22,'Belgium','BE','BEL')
,(23,'Belize','BZ','BLZ')
,(24,'Benin','BJ','BEN')
,(25,'Bermuda','BM','BMU')
,(26,'Bhutan','BT','BTN')
,(27,'Bolivia','BO','BOL')
,(28,'Bonaire, Sint Eustatius and Saba','BQ','BES')
,(29,'Bosnia and Herzegovina','BA','BIH')
,(30,'Botswana','BW','BWA')
,(31,'Bouvet Island','BV','BVT')
,(32,'Brazil','BR','BRA')
,(33,'British Indian Ocean Territory','IO','IOT')
,(34,'Brunei','BN','BRN')
,(35,'Bulgaria','BG','BGR')
,(36,'Burkina Faso','BF','BFA')
,(37,'Burundi','BI','BDI')
,(38,'Cambodia','KH','KHM')
,(39,'Cameroon','CM','CMR')
,(40,'Canada','CA','CAN')
,(41,'Cape Verde','CV','CPV')
,(42,'Cayman Islands','KY','CYM')
,(43,'Central African Republic','CF','CAF')
,(44,'Chad','TD','TCD')
,(45,'Chile','CL','CHL')
,(46,'China','CN','CHN')
,(47,'Christmas Island','CX','CXR')
,(48,'Cocos (Keeling) Islands','CC','CCK')
,(49,'Colombia','CO','COL')
,(50,'Comoros','KM','COM')
,(51,'Congo','CG','COG')
,(52,'Cook Islands','CK','COK')
,(53,'Costa Rica','CR','CRI')
,(54,'Ivory Coast','CI','CIV')
,(55,'Croatia','HR','HRV')
,(56,'Cuba','CU','CUB')
,(57,'Curacao','CW','CUW')
,(58,'Cyprus','CY','CYP')
,(59,'Czech Republic','CZ','CZE')
,(60,'Democratic Republic of the Congo','CD','COD')
,(61,'Denmark','DK','DNK')
,(62,'Djibouti','DJ','DJI')
,(63,'Dominica','DM','DMA')
,(64,'Dominican Republic','DO','DOM')
,(65,'Ecuador','EC','ECU')
,(66,'Egypt','EG','EGY')
,(67,'El Salvador','SV','SLV')
,(68,'Equatorial Guinea','GQ','GNQ')
,(69,'Eritrea','ER','ERI')
,(70,'Estonia','EE','EST')
,(71,'Ethiopia','ET','ETH')
,(72,'Falkland Islands (Malvinas)','FK','FLK')
,(73,'Faroe Islands','FO','FRO')
,(74,'Fiji','FJ','FJI')
,(75,'Finland','FI','FIN')
,(76,'France','FR','FRA')
,(77,'French Guiana','GF','GUF')
,(78,'French Polynesia','PF','PYF')
,(79,'French Southern Territories','TF','ATF')
,(80,'Gabon','GA','GAB')
,(81,'Gambia','GM','GMB')
,(82,'Georgia','GE','GEO')
,(83,'Germany','DE','DEU')
,(84,'Ghana','GH','GHA')
,(85,'Gibraltar','GI','GIB')
,(86,'Greece','GR','GRC')
,(87,'Greenland','GL','GRL')
,(88,'Grenada','GD','GRD')
,(89,'Guadaloupe','GP','GLP')
,(90,'Guam','GU','GUM')
,(91,'Guatemala','GT','GTM')
,(92,'Guernsey','GG','GGY')
,(93,'Guinea','GN','GIN')
,(94,'Guinea-Bissau','GW','GNB')
,(95,'Guyana','GY','GUY')
,(96,'Haiti','HT','HTI')
,(97,'Heard Island and McDonald Islands','HM','HMD')
,(98,'Honduras','HN','HND')
,(99,'Hong Kong','HK','HKG')
,(100,'Hungary','HU','HUN')
,(101,'Iceland','IS','ISL')
,(102,'India','IN','IND')
,(103,'Indonesia','ID','IDN')
,(104,'Iran','IR','IRN')
,(105,'Iraq','IQ','IRQ')
,(106,'Ireland','IE','IRL')
,(107,'Isle of Man','IM','IMN')
,(108,'Israel','IL','ISR')
,(109,'Italy','IT','ITA')
,(110,'Jamaica','JM','JAM')
,(111,'Japan','JP','JPN')
,(112,'Jersey','JE','JEY')
,(113,'Jordan','JO','JOR')
,(114,'Kazakhstan','KZ','KAZ')
,(115,'Kenya','KE','KEN')
,(116,'Kiribati','KI','KIR')
,(117,'Kosovo','XK','---')
,(118,'Kuwait','KW','KWT')
,(119,'Kyrgyzstan','KG','KGZ')
,(120,'Laos','LA','LAO')
,(121,'Latvia','LV','LVA')
,(122,'Lebanon','LB','LBN')
,(123,'Lesotho','LS','LSO')
,(124,'Liberia','LR','LBR')
,(125,'Libya','LY','LBY')
,(126,'Liechtenstein','LI','LIE')
,(127,'Lithuania','LT','LTU')
,(128,'Luxembourg','LU','LUX')
,(129,'Macao','MO','MAC')
,(130,'Macedonia','MK','MKD')
,(131,'Madagascar','MG','MDG')
,(132,'Malawi','MW','MWI')
,(133,'Malaysia','MY','MYS')
,(134,'Maldives','MV','MDV')
,(135,'Mali','ML','MLI')
,(136,'Malta','MT','MLT')
,(137,'Marshall Islands','MH','MHL')
,(138,'Martinique','MQ','MTQ')
,(139,'Mauritania','MR','MRT')
,(140,'Mauritius','MU','MUS')
,(141,'Mayotte','YT','MYT')
,(142,'Mexico','MX','MEX')
,(143,'Micronesia','FM','FSM')
,(144,'Moldava','MD','MDA')
,(145,'Monaco','MC','MCO')
,(146,'Mongolia','MN','MNG')
,(147,'Montenegro','ME','MNE')
,(148,'Montserrat','MS','MSR')
,(149,'Morocco','MA','MAR')
,(150,'Mozambique','MZ','MOZ')
,(151,'Myanmar (Burma)','MM','MMR')
,(152,'Namibia','NA','NAM')
,(153,'Nauru','NR','NRU')
,(154,'Nepal','NP','NPL')
,(155,'Netherlands','NL','NLD')
,(156,'New Caledonia','NC','NCL')
,(157,'New Zealand','NZ','NZL')
,(158,'Nicaragua','NI','NIC')
,(159,'Niger','NE','NER')
,(160,'Nigeria','NG','NGA')
,(161,'Niue','NU','NIU')
,(162,'Norfolk Island','NF','NFK')
,(163,'North Korea','KP','PRK')
,(164,'Northern Mariana Islands','MP','MNP')
,(165,'Norway','NO','NOR')
,(166,'Oman','OM','OMN')
,(167,'Pakistan','PK','PAK')
,(168,'Palau','PW','PLW')
,(169,'Palestine','PS','PSE')
,(170,'Panama','PA','PAN')
,(171,'Papua New Guinea','PG','PNG')
,(172,'Paraguay','PY','PRY')
,(173,'Peru','PE','PER')
,(174,'Phillipines','PH','PHL')
,(175,'Pitcairn','PN','PCN')
,(176,'Poland','PL','POL')
,(177,'Portugal','PT','PRT')
,(178,'Puerto Rico','PR','PRI')
,(179,'Qatar','QA','QAT')
,(180,'Reunion','RE','REU')
,(181,'Romania','RO','ROU')
,(182,'Russia','RU','RUS')
,(183,'Rwanda','RW','RWA')
,(184,'Saint Barthelemy','BL','BLM')
,(185,'Saint Helena','SH','SHN')
,(186,'Saint Kitts and Nevis','KN','KNA')
,(187,'Saint Lucia','LC','LCA')
,(188,'Saint Martin','MF','MAF')
,(189,'Saint Pierre and Miquelon','PM','SPM')
,(190,'Saint Vincent and the Grenadines','VC','VCT')
,(191,'Samoa','WS','WSM')
,(192,'San Marino','SM','SMR')
,(193,'Sao Tome and Principe','ST','STP')
,(194,'Saudi Arabia','SA','SAU')
,(195,'Senegal','SN','SEN')
,(196,'Serbia','RS','SRB')
,(197,'Seychelles','SC','SYC')
,(198,'Sierra Leone','SL','SLE')
,(199,'Singapore','SG','SGP')
,(200,'Sint Maarten','SX','SXM')
,(201,'Slovakia','SK','SVK')
,(202,'Slovenia','SI','SVN')
,(203,'Solomon Islands','SB','SLB')
,(204,'Somalia','SO','SOM')
,(205,'South Africa','ZA','ZAF')
,(206,'South Georgia and the South Sandwich Islands','GS','SGS')
,(207,'South Korea','KR','KOR')
,(208,'South Sudan','SS','SSD')
,(209,'Spain','ES','ESP')
,(210,'Sri Lanka','LK','LKA')
,(211,'Sudan','SD','SDN')
,(212,'Suriname','SR','SUR')
,(213,'Svalbard and Jan Mayen','SJ','SJM')
,(214,'Swaziland','SZ','SWZ')
,(215,'Sweden','SE','SWE')
,(216,'Switzerland','CH','CHE')
,(217,'Syria','SY','SYR')
,(218,'Taiwan','TW','TWN')
,(219,'Tajikistan','TJ','TJK')
,(220,'Tanzania','TZ','TZA')
,(221,'Thailand','TH','THA')
,(222,'Timor-Leste (East Timor)','TL','TLS')
,(223,'Togo','TG','TGO')
,(224,'Tokelau','TK','TKL')
,(225,'Tonga','TO','TON')
,(226,'Trinidad and Tobago','TT','TTO')
,(227,'Tunisia','TN','TUN')
,(228,'Turkey','TR','TUR')
,(229,'Turkmenistan','TM','TKM')
,(230,'Turks and Caicos Islands','TC','TCA')
,(231,'Tuvalu','TV','TUV')
,(232,'Uganda','UG','UGA')
,(233,'Ukraine','UA','UKR')
,(234,'United Arab Emirates','AE','ARE')
,(235,'United Kingdom','GB','GBR')
,(236,'United States','US','USA')
,(237,'United States Minor Outlying Islands','UM','UMI')
,(238,'Uruguay','UY','URY')
,(239,'Uzbekistan','UZ','UZB')
,(240,'Vanuatu','VU','VUT')
,(241,'Vatican City','VA','VAT')
,(242,'Venezuela','VE','VEN')
,(243,'Vietnam','VN','VNM')
,(244,'Virgin Islands, British','VG','VGB')
,(245,'Virgin Islands, US','VI','VIR')
,(246,'Wallis and Futuna','WF','WLF')
,(247,'Western Sahara','EH','ESH')
,(248,'Yemen','YE','YEM')
,(249,'Zambia','ZM','ZMB')
,(250,'Zimbabwe','ZW','ZWE');</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/05/insert-script-for-all-countries/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
