<?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>SQL Server &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/sql-server/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>Mon, 24 Jun 2019 19:42:10 +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>SQL Server: How to compare your objects between two Databases</title>
		<link>https://www.dbrnd.com/2019/08/sql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases/</link>
					<comments>https://www.dbrnd.com/2019/08/sql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 05 Aug 2019 16:16:04 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[compare]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[foreign key]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[Primary key]]></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[stored procedures]]></category>
		<category><![CDATA[tables]]></category>
		<category><![CDATA[trigger]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[views]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4094</guid>

					<description><![CDATA[When we are working with different versions of database, database professionals are comparing the objects between databases of SQL Server. They must make sure about that all databases are in sync. Using this T-SQL Script, you can compare two databases and find a list of unmatched objects like: Constraints, Tables, Views, Stored Procedure, Triggers. 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&#038;title=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" data-a2a-url="https://www.dbrnd.com/2019/08/sql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases/" data-a2a-title="SQL Server: How to compare your objects between two Databases"></a></p><p>When we are working with different versions of database, database professionals are comparing the objects between databases of SQL Server.<br />
They must make sure about that all databases are in sync. </p>
<blockquote><p>Using this T-SQL Script, you can compare two databases and find a list of unmatched objects like: Constraints, Tables, Views, Stored Procedure, Triggers. </p></blockquote>
<p><strong>Sample demonstration:</strong><br />
<strong>First, Create two sample databases:<br />
</strong></p>
<pre class="crayon-plain-tag">CREATE DATABASE ABC
GO
CREATE DATABASE XYZ
GO</pre> </p>
<p><strong>Create few sample tables in both the databases:<br />
</strong></p><pre class="crayon-plain-tag">CREATE TABLE ABC.dbo.tbl_Test
(
	ID INT
	,MyDate DATE
)
GO

CREATE TABLE ABC.dbo.tbl_MyTest
(
	ID INT
	,Name VARCHAR(50)
)
GO

CREATE TABLE XYZ.dbo.tbl_Test
(
	ID INT
	,MyDate DATE
)
GO

CREATE TABLE XYZ.dbo.tbl_MyFinal
(
	ID INT
	,Name VARCHAR(50)
)
GO</pre><p> </p>
<p><strong>T-SQL Script to compare objects between two databases:</strong><br />
According to your databases, You can change the name of @SourceDatabase and @DestinationDatabase.</p>
<pre class="crayon-plain-tag">DECLARE @SourceDatabase VARCHAR(50) 
DECLARE @DestinationDatabase VARCHAR(50) 
DECLARE @SQL VARCHAR(MAX) 
     
SELECT @SourceDatabase = 'ABC' 
SELECT @DestinationDatabase = 'XYZ'
     
SELECT @SQL = 
'
	SELECT 
		ISNULL(S.name,D.name) ObjectName
		,CASE  
			WHEN S.object_id IS NULL 
				THEN D.type_desc + '' is missing in the Source Database: '	+ @SourceDatabase + ''' 
			WHEN D.object_id IS NULL 
				THEN S.type_desc +  '' is missing in the Destination Database: ' + @DestinationDatabase + ''' 
		END ''Status'' 
	FROM 
	(
		SELECT * FROM ' + @SourceDatabase + '.SYS.objects  
		WHERE Type_desc not in (''INTERNAL_TABLE'',''SYSTEM_TABLE'',''SERVICE_QUEUE'')
	) AS S  
	FULL OUTER JOIN 
	(
		SELECT * FROM ' + @DestinationDatabase + '.SYS.objects  
		WHERE Type_desc not in (''INTERNAL_TABLE'',''SYSTEM_TABLE'',''SERVICE_QUEUE'')
	) AS D 
	ON S.name = D.name 
		AND S.type = D.type 
	ORDER BY isnull(S.type,D.type)
'  
EXEC (@Sql)</pre> </p>
<p><strong>The Result:</strong></p>
<pre class="crayon-plain-tag">ObjectName                         Status
------------------  -------------------------------------------------------
tbl_Test              NULL
tbl_MyFinal           USER_TABLE is missing in the Source Database: ABC
tbl_MyTest            USER_TABLE is missing in the Destination Database: XYZ</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/08/sql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Script to check Network or Connect permission of User</title>
		<link>https://www.dbrnd.com/2019/06/sql-server-script-to-check-network-or-connect-permission-of-user/</link>
					<comments>https://www.dbrnd.com/2019/06/sql-server-script-to-check-network-or-connect-permission-of-user/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 24 Jun 2019 16:16:20 +0000</pubDate>
				<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[dbrnd]]></category>
		<category><![CDATA[endpoints]]></category>
		<category><![CDATA[server_permissions]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7263</guid>

					<description><![CDATA[In this post, I am sharing a script for checking the Network and Connect related permission of SQL Server User. When you installed the SQL Server, for network access you should enable the TCP/IP pipeline and other connect permission so that others can access the server. Using below script, you can easily identify those permissions [&#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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&#038;title=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" data-a2a-url="https://www.dbrnd.com/2019/06/sql-server-script-to-check-network-or-connect-permission-of-user/" data-a2a-title="SQL Server: Script to check Network or Connect permission of User"></a></p><p>In this post, I am sharing a script for checking the Network and Connect related permission of SQL Server User. </p>
<p>When you installed the SQL Server, for network access you should enable the TCP/IP pipeline and other connect permission so that others can access the server. </p>
<p>Using below script, you can easily identify those permissions which are important for connecting SQL Server. I took the reference from <a href="https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/troubleshoot-always-on-availability-groups-configuration-sql-server" rel="noopener" target="_blank">this</a> official page. </p>
<pre class="crayon-plain-tag">SELECT 
	EP.name, 
	SP.STATE,   
	CONVERT(nvarchar(38), suser_name(SP.grantor_principal_id)) AS GRANTOR,   
	SP.TYPE AS PERMISSION,  
	CONVERT(nvarchar(46),suser_name(SP.grantee_principal_id)) AS GRANTEE   
FROM sys.server_permissions SP, sys.endpoints EP  
WHERE SP.major_id = EP.endpoint_id  
ORDER BY Permission,grantor, grantee</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/06/sql-server-script-to-check-network-or-connect-permission-of-user/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Script to find the total size of the Indexes</title>
		<link>https://www.dbrnd.com/2019/05/sql-server-script-to-find-the-total-size-of-the-indexes/</link>
					<comments>https://www.dbrnd.com/2019/05/sql-server-script-to-find-the-total-size-of-the-indexes/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 27 May 2019 16:16:18 +0000</pubDate>
				<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[dbrnd]]></category>
		<category><![CDATA[duplicate index]]></category>
		<category><![CDATA[fragmentation]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[redundant index]]></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=4191</guid>

					<description><![CDATA[In this post, I am sharing one script to find the size of Indexes in SQL Server. The first step for performance optimization is to measure the size of tables and indexes which helps you to find out more about fragmentation. I have already shared a couple of articles on SQL Server Indexes like: find [&#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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&#038;title=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" data-a2a-url="https://www.dbrnd.com/2019/05/sql-server-script-to-find-the-total-size-of-the-indexes/" data-a2a-title="SQL Server: Script to find the total size of the Indexes"></a></p><p>In this post, I am sharing one script to find the size of Indexes in SQL Server.</p>
<blockquote><p>The first step for performance optimization is to measure the size of tables and indexes which helps you to find out more about fragmentation.</p></blockquote>
<p>I have already shared a couple of articles on SQL Server Indexes like: find duplicate indexes, unused indexes and fragmentation of indexes. </p>
<p>You can also access these articles after below T-SQL. </p>
<pre class="crayon-plain-tag">SELECT
	OBJECT_SCHEMA_NAME(i.OBJECT_ID) AS SchemaName
	,OBJECT_NAME(i.OBJECT_ID) AS TableName
	,i.name AS IndexName
	,8 * SUM(au.used_pages) AS IndexSizeInKB
FROM sys.indexes AS i
JOIN sys.partitions AS p 
	ON p.OBJECT_ID = i.OBJECT_ID 
		AND p.index_id = i.index_id
JOIN sys.allocation_units AS au 
	ON au.container_id = p.partition_id
GROUP BY 
	i.OBJECT_ID
	,i.index_id
	,i.name
ORDER BY OBJECT_NAME(i.OBJECT_ID),i.index_id</pre> </p>
<p><strong>Other related articles:</strong></p>
<blockquote class="wp-embedded-content" data-secret="iWrp53ltXi"><p><a href="https://www.dbrnd.com/2016/04/sql-server-find-the-total-row-count-and-size-of-the-tables/">SQL Server: Find the total row count and size of the Tables</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/04/sql-server-find-the-total-row-count-and-size-of-the-tables/embed/#?secret=iWrp53ltXi" data-secret="iWrp53ltXi" width="600" height="338" title="&#8220;SQL Server: Find the total row count and size of the Tables&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="kFuNrnjgJI"><p><a href="https://www.dbrnd.com/2016/11/sql-server-script-to-find-redundant-and-duplicate-indexes/">SQL Server: Script to find Redundant and Duplicate Indexes</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/11/sql-server-script-to-find-redundant-and-duplicate-indexes/embed/#?secret=kFuNrnjgJI" data-secret="kFuNrnjgJI" width="600" height="338" title="&#8220;SQL Server: Script to find Redundant and Duplicate Indexes&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="CLStlFpaQE"><p><a href="https://www.dbrnd.com/2015/10/sql-server-script-to-find-usage-of-database-indexes/">SQL Server: Script to find usage of Database Indexes</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/10/sql-server-script-to-find-usage-of-database-indexes/embed/#?secret=CLStlFpaQE" data-secret="CLStlFpaQE" width="600" height="338" title="&#8220;SQL Server: Script to find usage of Database Indexes&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="CuRotuXr6g"><p><a href="https://www.dbrnd.com/2016/03/sql-server-script-to-find-index-average-fragmentation-in-percentage/">SQL Server: Script to find Index Average Fragmentation in Percentage</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/03/sql-server-script-to-find-index-average-fragmentation-in-percentage/embed/#?secret=CuRotuXr6g" data-secret="CuRotuXr6g" width="600" height="338" title="&#8220;SQL Server: Script to find Index Average Fragmentation in Percentage&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/05/sql-server-script-to-find-the-total-size-of-the-indexes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: How to find a time when was SQL Server Restarted</title>
		<link>https://www.dbrnd.com/2019/05/sql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces/</link>
					<comments>https://www.dbrnd.com/2019/05/sql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 20 May 2019 16:16:01 +0000</pubDate>
				<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[dbrnd]]></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 Restart]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[sys.databases]]></category>
		<category><![CDATA[sys.dm_exec_sessions]]></category>
		<category><![CDATA[sys.traces]]></category>
		<category><![CDATA[sysdatabases]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4089</guid>

					<description><![CDATA[In this post, I am sharing few different scripts to find when was the SQL Server restarted. The DBAs must know about this because they have to make sure about data consistency. For example: when SQL Server restarted because of power failure, system failure, update failure or any other reason, We must check the last [&#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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&#038;title=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" data-a2a-url="https://www.dbrnd.com/2019/05/sql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces/" data-a2a-title="SQL Server: How to find a time when was SQL Server Restarted"></a></p><p>In this post, I am sharing few different scripts to find when was the SQL Server restarted.</p>
<blockquote><p>The DBAs must know about this because they have to make sure about data consistency.
</p></blockquote>
<blockquote><p>For example: when SQL Server restarted because of power failure, system failure, update failure or any other reason, We must check the last restarted time of SQL Server. Because from that time, we have to restore data from our backup disks to the actual disk.</p></blockquote>
<blockquote><p>When SQL Server restarted, few transactions also rollback. We have to also identify these transactions by knowing the correct time when SQL Server actually restarted.
</p></blockquote>
<p><strong>Using sys.dm_exec_sessions:</strong></p>
<pre class="crayon-plain-tag">SELECT login_time FROM sys.dm_exec_sessions WHERE session_id = 1
GO</pre> </p>
<p><strong>Using sys.dm_os_sys_info:</strong></p>
<pre class="crayon-plain-tag">SELECT sqlserver_start_time FROM sys.dm_os_sys_info
GO</pre> </p>
<p><strong>Using sys.databases:</strong></p>
<pre class="crayon-plain-tag">SELECT create_date FROM sys.databases WHERE name = 'tempdb' 
GO</pre> </p>
<p><strong>Using sys.traces:</strong></p>
<pre class="crayon-plain-tag">SELECT start_time FROM sys.traces WHERE is_default = 1  
GO</pre> </p>
<p><strong>Using sysdatabases: </strong></p>
<pre class="crayon-plain-tag">SELECT crdate FROM sysdatabases WHERE name='tempdb'  
GO</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/05/sql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server Interview: How to remove New Line Character from a string?</title>
		<link>https://www.dbrnd.com/2019/04/sql-server-interview-how-to-remove-new-line-character-from-a-string/</link>
					<comments>https://www.dbrnd.com/2019/04/sql-server-interview-how-to-remove-new-line-character-from-a-string/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 29 Apr 2019 16:16:57 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Remove New Line character]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[String Operation]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7413</guid>

					<description><![CDATA[In this post, I am sharing a SQL Interview questions for Database Developer. If you are database fresher and working with SQL, you must prepare for string/date operations related questions and answers. One of the example questions is How to remove New Line Character from a string? Check the below sample code: [crayon-6ab2f8df61d79425710314/]]]></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%2F2019%2F04%2Fsql-server-interview-how-to-remove-new-line-character-from-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20remove%20New%20Line%20Character%20from%20a%20string%3F" 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%2F2019%2F04%2Fsql-server-interview-how-to-remove-new-line-character-from-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20remove%20New%20Line%20Character%20from%20a%20string%3F" 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%2F2019%2F04%2Fsql-server-interview-how-to-remove-new-line-character-from-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20remove%20New%20Line%20Character%20from%20a%20string%3F" 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%2F2019%2F04%2Fsql-server-interview-how-to-remove-new-line-character-from-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20remove%20New%20Line%20Character%20from%20a%20string%3F" 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%2F2019%2F04%2Fsql-server-interview-how-to-remove-new-line-character-from-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20remove%20New%20Line%20Character%20from%20a%20string%3F" 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%2F2019%2F04%2Fsql-server-interview-how-to-remove-new-line-character-from-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20remove%20New%20Line%20Character%20from%20a%20string%3F" 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%2F2019%2F04%2Fsql-server-interview-how-to-remove-new-line-character-from-a-string%2F&#038;title=SQL%20Server%20Interview%3A%20How%20to%20remove%20New%20Line%20Character%20from%20a%20string%3F" data-a2a-url="https://www.dbrnd.com/2019/04/sql-server-interview-how-to-remove-new-line-character-from-a-string/" data-a2a-title="SQL Server Interview: How to remove New Line Character from a string?"></a></p><p>In this post, I am sharing a SQL Interview questions for Database Developer. If you are database fresher and working with SQL, you must prepare for string/date operations related questions and answers. </p>
<p>One of the example questions is How to remove New Line Character from a string?</p>
<p><strong>Check the below sample code:</strong></p><pre class="crayon-plain-tag">DECLARE @str VARCHAR(100)
SET @str = 'I am Database 
Engineer'

SELECT @str AS With_New_Line

With_New_Line
---------------------
I am Database 
Engineer

SELECT REPLACE(REPLACE(@str, CHAR(13), ''), CHAR(10), '') AS Without_New_Line

Without_New_Line
---------------------------
I am Database Engineer</pre><p> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/04/sql-server-interview-how-to-remove-new-line-character-from-a-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Find Stored Procedures which use Dynamic SQL and Temporary Table</title>
		<link>https://www.dbrnd.com/2019/04/sql-server-find-stored-procedures-which-use-dynamic-sql-and-temporary-table/</link>
					<comments>https://www.dbrnd.com/2019/04/sql-server-find-stored-procedures-which-use-dynamic-sql-and-temporary-table/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 08 Apr 2019 18:30:46 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Data Security]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[dynamic sql]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[SQL Injection]]></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[Stored Procedure]]></category>
		<category><![CDATA[Temp Table]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4076</guid>

					<description><![CDATA[Whenever you are searching for SQL query optimization tips or prevent SQL injection tips, you can find few common suggestions like: Please do not use Dynamic SQL in your Stored Procedure. Please try to avoid the use of Temp Tables. I always tell to database developers, please try to avoid the use of Dynamic SQL [&#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%2F2019%2F04%2Fsql-server-find-stored-procedures-which-use-dynamic-sql-and-temporary-table%2F&amp;linkname=SQL%20Server%3A%20Find%20Stored%20Procedures%20which%20use%20Dynamic%20SQL%20and%20Temporary%20Table" 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%2F2019%2F04%2Fsql-server-find-stored-procedures-which-use-dynamic-sql-and-temporary-table%2F&amp;linkname=SQL%20Server%3A%20Find%20Stored%20Procedures%20which%20use%20Dynamic%20SQL%20and%20Temporary%20Table" 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%2F2019%2F04%2Fsql-server-find-stored-procedures-which-use-dynamic-sql-and-temporary-table%2F&amp;linkname=SQL%20Server%3A%20Find%20Stored%20Procedures%20which%20use%20Dynamic%20SQL%20and%20Temporary%20Table" 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%2F2019%2F04%2Fsql-server-find-stored-procedures-which-use-dynamic-sql-and-temporary-table%2F&amp;linkname=SQL%20Server%3A%20Find%20Stored%20Procedures%20which%20use%20Dynamic%20SQL%20and%20Temporary%20Table" 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%2F2019%2F04%2Fsql-server-find-stored-procedures-which-use-dynamic-sql-and-temporary-table%2F&amp;linkname=SQL%20Server%3A%20Find%20Stored%20Procedures%20which%20use%20Dynamic%20SQL%20and%20Temporary%20Table" 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%2F2019%2F04%2Fsql-server-find-stored-procedures-which-use-dynamic-sql-and-temporary-table%2F&amp;linkname=SQL%20Server%3A%20Find%20Stored%20Procedures%20which%20use%20Dynamic%20SQL%20and%20Temporary%20Table" 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%2F2019%2F04%2Fsql-server-find-stored-procedures-which-use-dynamic-sql-and-temporary-table%2F&#038;title=SQL%20Server%3A%20Find%20Stored%20Procedures%20which%20use%20Dynamic%20SQL%20and%20Temporary%20Table" data-a2a-url="https://www.dbrnd.com/2019/04/sql-server-find-stored-procedures-which-use-dynamic-sql-and-temporary-table/" data-a2a-title="SQL Server: Find Stored Procedures which use Dynamic SQL and Temporary Table"></a></p><p>Whenever you are searching for SQL query optimization tips or prevent SQL injection tips, you can find few common suggestions like: Please do not use Dynamic SQL in your Stored Procedure. Please try to avoid the use of Temp Tables.</p>
<blockquote><p>I always tell to database developers, please try to avoid the use of Dynamic SQL and Temp Table in your Stored Procedures.</p></blockquote>
<blockquote><p>A Temp Table requires additional CPU/IO which degrades the query performance. A Dynamic SQL is not safe against the SQL Injection and It also compiles data every time which requires more Memory. </p></blockquote>
<blockquote><p>Here, I am sharing one of the important scripts which gives you list of SQL Server Stored Procedures, which use the Dynamic SQL and Temp Table.<br />
You can easily get the list of Stored Procedures and can make plans of modifying it.</p></blockquote>
<blockquote><p>Prepared this script using sys.dm_exec_describe_first_result_set and sys.procedures where error number 4 = Dynamic SQL and error number 10 = Temporary Table.</p></blockquote>
<pre class="crayon-plain-tag">SELECT  
	SP_Name
	,error_message
	,error_type_desc 
FROM (SELECT schema_name(schema_id)+'.'+name SP_Name FROM sys.procedures) AS T
CROSS APPLY sys.dm_exec_describe_first_result_set (SP_Name, NULL, 0) 
WHERE error_type IN (4,10)</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/04/sql-server-find-stored-procedures-which-use-dynamic-sql-and-temporary-table/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Auto Generate Database Detach and Attach Scripts</title>
		<link>https://www.dbrnd.com/2019/01/sql-server-auto-generate-database-detach-and-attach-scripts-single_user-multi_user-database-mode-data-migration/</link>
					<comments>https://www.dbrnd.com/2019/01/sql-server-auto-generate-database-detach-and-attach-scripts-single_user-multi_user-database-mode-data-migration/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 28 Jan 2019 16:16:23 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[attach]]></category>
		<category><![CDATA[data migration]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[detach]]></category>
		<category><![CDATA[multi_user]]></category>
		<category><![CDATA[single_user]]></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=4070</guid>

					<description><![CDATA[In this post, I am sharing two different scripts to populate attach and detach scripts for all your SQL Server Databases. When we are migrating our SQL Database Server, we require to move all databases from one server to another server. If you have a big number of databases, making of individual attach and detach [&#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%2F2019%2F01%2Fsql-server-auto-generate-database-detach-and-attach-scripts-single_user-multi_user-database-mode-data-migration%2F&amp;linkname=SQL%20Server%3A%20Auto%20Generate%20Database%20Detach%20and%20Attach%20Scripts" 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%2F2019%2F01%2Fsql-server-auto-generate-database-detach-and-attach-scripts-single_user-multi_user-database-mode-data-migration%2F&amp;linkname=SQL%20Server%3A%20Auto%20Generate%20Database%20Detach%20and%20Attach%20Scripts" 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%2F2019%2F01%2Fsql-server-auto-generate-database-detach-and-attach-scripts-single_user-multi_user-database-mode-data-migration%2F&amp;linkname=SQL%20Server%3A%20Auto%20Generate%20Database%20Detach%20and%20Attach%20Scripts" 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%2F2019%2F01%2Fsql-server-auto-generate-database-detach-and-attach-scripts-single_user-multi_user-database-mode-data-migration%2F&amp;linkname=SQL%20Server%3A%20Auto%20Generate%20Database%20Detach%20and%20Attach%20Scripts" 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%2F2019%2F01%2Fsql-server-auto-generate-database-detach-and-attach-scripts-single_user-multi_user-database-mode-data-migration%2F&amp;linkname=SQL%20Server%3A%20Auto%20Generate%20Database%20Detach%20and%20Attach%20Scripts" 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%2F2019%2F01%2Fsql-server-auto-generate-database-detach-and-attach-scripts-single_user-multi_user-database-mode-data-migration%2F&amp;linkname=SQL%20Server%3A%20Auto%20Generate%20Database%20Detach%20and%20Attach%20Scripts" 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%2F2019%2F01%2Fsql-server-auto-generate-database-detach-and-attach-scripts-single_user-multi_user-database-mode-data-migration%2F&#038;title=SQL%20Server%3A%20Auto%20Generate%20Database%20Detach%20and%20Attach%20Scripts" data-a2a-url="https://www.dbrnd.com/2019/01/sql-server-auto-generate-database-detach-and-attach-scripts-single_user-multi_user-database-mode-data-migration/" data-a2a-title="SQL Server: Auto Generate Database Detach and Attach Scripts"></a></p><p>In this post, I am sharing two different scripts to populate attach and detach scripts for all your SQL Server Databases.</p>
<blockquote><p>When we are migrating our SQL Database Server, we require to move all databases from one server to another server.</p>
<p>If you have a big number of databases, making of individual attach and detach script will take more time. </p></blockquote>
<blockquote><p>I have also added script to change database access mode like: Multi_User to Single_User for Detach Process and Single_User to Multi_User for Attach Process. </p>
<p>If your database is in use, you may require to change the database access mode to close all running connections.</p></blockquote>
<p>Using this script you can populate attach and detach scripts for all databases.<br />
<strong>Execute this script to generate detach scripts for all databases:</strong></p>
<pre class="crayon-plain-tag">DECLARE @DatabaseName AS VARCHAR(255)
DECLARE CurDetach CURSOR FOR
  
  SELECT name
  FROM   MASTER.sys.databases
  WHERE  owner_sid &gt; 1;

OPEN CurDetach
FETCH Next FROM CurDetach INTO @DatabaseName
WHILE @@FETCH_STATUS = 0
  BEGIN      
	  PRINT 
		'ALTER DATABASE ' + @DatabaseName + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE' 
		+ CHAR(10) + ' GO' + CHAR(10) +
		'sp_detach_db ' + @DatabaseName + CHAR(10)
		+ ' GO' + CHAR(10) + 'Print ''Detach of '
		+ @DatabaseName
		+ ' database completed successfully'''
		+ CHAR(10) + ' GO'

      FETCH NEXT FROM CurDetach INTO @DatabaseName
  END

CLOSE CurDetach
DEALLOCATE CurDetach</pre> </p>
<p><strong>Execute this script to generate attach scripts for all databases:</strong><br />
You can change your file name or file path using @Filepath variable.</p>
<pre class="crayon-plain-tag">DECLARE @DatabaseName AS VARCHAR(255)
DECLARE @Filepath AS VARCHAR(255)

SET @Filepath = 'C:\SQLDATA\'

DECLARE CurAttach CURSOR FOR
  SELECT name
  FROM   MASTER.sys.databases
  WHERE  owner_sid &gt; 1;

OPEN CurAttach
FETCH Next FROM CurAttach INTO @DatabaseName
WHILE @@FETCH_STATUS = 0
  BEGIN      
	  PRINT 
		'CREATE DATABASE ' + @DatabaseName + ' ON
		(FILENAME = '+ @Filepath + @DatabaseName +'.mdf)
		,(FILENAME = '+ @Filepath + @DatabaseName +'.ldf)
		FOR ATTACH
		GO

		ALTER DATABASE '+ @DatabaseName +' SET MULTI_USER WITH ROLLBACK IMMEDIATE
		GO'
      FETCH NEXT FROM CurAttach INTO @DatabaseName
  END

CLOSE CurAttach
DEALLOCATE CurAttach</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/01/sql-server-auto-generate-database-detach-and-attach-scripts-single_user-multi_user-database-mode-data-migration/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Script to find a list of Weekends between two Dates</title>
		<link>https://www.dbrnd.com/2019/01/sql-server-script-to-find-a-list-of-weekends-between-two-dates-dba-maintenance-work/</link>
					<comments>https://www.dbrnd.com/2019/01/sql-server-script-to-find-a-list-of-weekends-between-two-dates-dba-maintenance-work/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 21 Jan 2019 07:09:51 +0000</pubDate>
				<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[dba activity]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[EOMONTH]]></category>
		<category><![CDATA[maintenance]]></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>
		<category><![CDATA[weekend]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4067</guid>

					<description><![CDATA[In this post, I am sharing one script to find a list of Weekends between given two Dates in SQL Server. If you are a database developer and reading this post, you should know about the life of DBA. Database Administrator life is for 24*7 and they have to also set few maintenance tasks over [&#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%2F2019%2F01%2Fsql-server-script-to-find-a-list-of-weekends-between-two-dates-dba-maintenance-work%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20a%20list%20of%20Weekends%20between%20two%20Dates" 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%2F2019%2F01%2Fsql-server-script-to-find-a-list-of-weekends-between-two-dates-dba-maintenance-work%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20a%20list%20of%20Weekends%20between%20two%20Dates" 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%2F2019%2F01%2Fsql-server-script-to-find-a-list-of-weekends-between-two-dates-dba-maintenance-work%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20a%20list%20of%20Weekends%20between%20two%20Dates" 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%2F2019%2F01%2Fsql-server-script-to-find-a-list-of-weekends-between-two-dates-dba-maintenance-work%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20a%20list%20of%20Weekends%20between%20two%20Dates" 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%2F2019%2F01%2Fsql-server-script-to-find-a-list-of-weekends-between-two-dates-dba-maintenance-work%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20a%20list%20of%20Weekends%20between%20two%20Dates" 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%2F2019%2F01%2Fsql-server-script-to-find-a-list-of-weekends-between-two-dates-dba-maintenance-work%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20a%20list%20of%20Weekends%20between%20two%20Dates" 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%2F2019%2F01%2Fsql-server-script-to-find-a-list-of-weekends-between-two-dates-dba-maintenance-work%2F&#038;title=SQL%20Server%3A%20Script%20to%20find%20a%20list%20of%20Weekends%20between%20two%20Dates" data-a2a-url="https://www.dbrnd.com/2019/01/sql-server-script-to-find-a-list-of-weekends-between-two-dates-dba-maintenance-work/" data-a2a-title="SQL Server: Script to find a list of Weekends between two Dates"></a></p><p>In this post, I am sharing one script to find a list of Weekends between given two Dates in SQL Server.</p>
<blockquote><p>If you are a database developer and reading this post, you should know about the life of DBA.<br />
Database Administrator life is for 24*7 and they have to also set few maintenance tasks over the weekends and holidays. </p></blockquote>
<blockquote><p>This script helps to DBA for finding available weekends between the dates so they can set auto-schedule for database maintenance.</p></blockquote>
<p><strong>Please access few related articles:</strong></p>
<blockquote class="wp-embedded-content" data-secret="VRCL4CschW"><p><a href="https://www.dbrnd.com/2016/07/a-day-in-the-life-of-a-dba-database-administrator/">A Day in the life of a DBA !</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/07/a-day-in-the-life-of-a-dba-database-administrator/embed/#?secret=VRCL4CschW" data-secret="VRCL4CschW" width="600" height="338" title="&#8220;A Day in the life of a DBA !&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="Fdmay8AehU"><p><a href="https://www.dbrnd.com/2016/01/sql-server-2012-find-first-sunday-of-next-month-using-eomonth/">SQL Server 2012: Find First Sunday of Next Month using EOMONTH</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/01/sql-server-2012-find-first-sunday-of-next-month-using-eomonth/embed/#?secret=Fdmay8AehU" data-secret="Fdmay8AehU" width="600" height="338" title="&#8220;SQL Server 2012: Find First Sunday of Next Month using EOMONTH&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<pre class="crayon-plain-tag">DECLARE @beginDate DATE = '20161201'
DECLARE @endDate DATE = '20161231'  
 
DECLARE @Weekend TABLE
(
	Weekend DATE PRIMARY KEY
	,IsWeekend BIT
)
 
WHILE @beginDate &lt;= @endDate 
BEGIN 
INSERT INTO @Weekend 
SELECT 
	@beginDate AS Weekend 
	,(CASE WHEN DATEPART(WEEKDAY, @beginDate) In (7, 1) THEN 1 ELSE 0 END) AS IsWeekend 
	SET @beginDate = DateAdd(Day, 1, @beginDate) 
END
 
SELECT Weekend FROM @Weekend WHERE IsWeekend = 1</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/01/sql-server-script-to-find-a-list-of-weekends-between-two-dates-dba-maintenance-work/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Script to take full backup for all the Databases</title>
		<link>https://www.dbrnd.com/2019/01/sql-server-script-to-take-full-backup-for-all-the-databases-ad-hoc-full-database-backup/</link>
					<comments>https://www.dbrnd.com/2019/01/sql-server-script-to-take-full-backup-for-all-the-databases-ad-hoc-full-database-backup/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 14 Jan 2019 07:03:32 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[auto backup]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database backup script]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[full backup]]></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=4041</guid>

					<description><![CDATA[In this post, I am sharing one T-SQL Script to take full backup for all the databases of SQL Server. There are different types of backups and techniques available in the SQL Server, but I always believe in DBA Scripts which you can modify as per your requirements. Apart from scheduled backups, sometimes DBA requires [&#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%2F2019%2F01%2Fsql-server-script-to-take-full-backup-for-all-the-databases-ad-hoc-full-database-backup%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20take%20full%20backup%20for%20all%20the%20Databases" 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%2F2019%2F01%2Fsql-server-script-to-take-full-backup-for-all-the-databases-ad-hoc-full-database-backup%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20take%20full%20backup%20for%20all%20the%20Databases" 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%2F2019%2F01%2Fsql-server-script-to-take-full-backup-for-all-the-databases-ad-hoc-full-database-backup%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20take%20full%20backup%20for%20all%20the%20Databases" 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%2F2019%2F01%2Fsql-server-script-to-take-full-backup-for-all-the-databases-ad-hoc-full-database-backup%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20take%20full%20backup%20for%20all%20the%20Databases" 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%2F2019%2F01%2Fsql-server-script-to-take-full-backup-for-all-the-databases-ad-hoc-full-database-backup%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20take%20full%20backup%20for%20all%20the%20Databases" 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%2F2019%2F01%2Fsql-server-script-to-take-full-backup-for-all-the-databases-ad-hoc-full-database-backup%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20take%20full%20backup%20for%20all%20the%20Databases" 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%2F2019%2F01%2Fsql-server-script-to-take-full-backup-for-all-the-databases-ad-hoc-full-database-backup%2F&#038;title=SQL%20Server%3A%20Script%20to%20take%20full%20backup%20for%20all%20the%20Databases" data-a2a-url="https://www.dbrnd.com/2019/01/sql-server-script-to-take-full-backup-for-all-the-databases-ad-hoc-full-database-backup/" data-a2a-title="SQL Server: Script to take full backup for all the Databases"></a></p><p>In this post, I am sharing one T-SQL Script to take full backup for all the databases of SQL Server. </p>
<blockquote><p>There are different types of backups and techniques available in the SQL Server, but I always believe in DBA Scripts which you can modify as per your requirements.
</p></blockquote>
<blockquote><p>Apart from scheduled backups, sometimes DBA requires ad-hoc full backups of databases which they can do using this script.</p></blockquote>
<p><strong>Script to take full backup of databases:</strong><br />
You can change the path name (&#8216;C:\TempBackup\&#8217;) and replace with your path name. </p>
<pre class="crayon-plain-tag">DECLARE @dbName	NVARCHAR (255); 
DECLARE @SQL NVARCHAR(4000); 
 
DECLARE DBBackupCur 
CURSOR FOR 
    SELECT name FROM sys.databases WITH (NOLOCK) 
    WHERE name NOT IN ('master','model','msdb','tempdb')  
    OPTION (RECOMPILE); 
 
OPEN DBBackupCur;     
FETCH NEXT FROM DBBackupCur INTO @dbName 
WHILE (@@fetch_status &lt;&gt; -1) 
BEGIN 
    IF (@@fetch_status &lt;&gt; -2) 
    BEGIN 
        SET @SQL = N'BACKUP DATABASE [' + @dbName + N'] TO DISK = ''C:\TempBackup\' + @dbName + N'.bak'''; 
        EXECUTE sp_executesql @SQL 
        PRINT N'Backup completed: ' + @dbName 
    END; 
    FETCH NEXT FROM DBBackupCur INTO @dbName; 
END; 
CLOSE DBBackupCur; 
DEALLOCATE DBBackupCur; 
GO</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/01/sql-server-script-to-take-full-backup-for-all-the-databases-ad-hoc-full-database-backup/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Set IDENTITY to existing Column of a Table</title>
		<link>https://www.dbrnd.com/2019/01/sql-server-set-identity-to-existing-column-of-a-table/</link>
					<comments>https://www.dbrnd.com/2019/01/sql-server-set-identity-to-existing-column-of-a-table/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 07 Jan 2019 16:16:24 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[ALTER COLUMN]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Identity Column]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7389</guid>

					<description><![CDATA[Last week, I was exploring few SQL Server forums and found an interesting discussion on the IDENTITY Column of SQL Server. One of the discussions like How to set IDENTITY to existing column of a Table? Actually, there is no any specific option for setting IDENTITY to existing Column of a Table. But there are [&#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%2F2019%2F01%2Fsql-server-set-identity-to-existing-column-of-a-table%2F&amp;linkname=SQL%20Server%3A%20Set%20IDENTITY%20to%20existing%20Column%20of%20a%20Table" 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%2F2019%2F01%2Fsql-server-set-identity-to-existing-column-of-a-table%2F&amp;linkname=SQL%20Server%3A%20Set%20IDENTITY%20to%20existing%20Column%20of%20a%20Table" 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%2F2019%2F01%2Fsql-server-set-identity-to-existing-column-of-a-table%2F&amp;linkname=SQL%20Server%3A%20Set%20IDENTITY%20to%20existing%20Column%20of%20a%20Table" 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%2F2019%2F01%2Fsql-server-set-identity-to-existing-column-of-a-table%2F&amp;linkname=SQL%20Server%3A%20Set%20IDENTITY%20to%20existing%20Column%20of%20a%20Table" 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%2F2019%2F01%2Fsql-server-set-identity-to-existing-column-of-a-table%2F&amp;linkname=SQL%20Server%3A%20Set%20IDENTITY%20to%20existing%20Column%20of%20a%20Table" 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%2F2019%2F01%2Fsql-server-set-identity-to-existing-column-of-a-table%2F&amp;linkname=SQL%20Server%3A%20Set%20IDENTITY%20to%20existing%20Column%20of%20a%20Table" 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%2F2019%2F01%2Fsql-server-set-identity-to-existing-column-of-a-table%2F&#038;title=SQL%20Server%3A%20Set%20IDENTITY%20to%20existing%20Column%20of%20a%20Table" data-a2a-url="https://www.dbrnd.com/2019/01/sql-server-set-identity-to-existing-column-of-a-table/" data-a2a-title="SQL Server: Set IDENTITY to existing Column of a Table"></a></p><p>Last week, I was exploring few SQL Server forums and found an interesting discussion on the IDENTITY Column of SQL Server.<br />
One of the discussions like How to set IDENTITY to existing column of a Table?</p>
<p>Actually, there is no any specific option for setting IDENTITY to existing Column of a Table.</p>
<p>But there are different tricks to do this, sharing one of the tricks here&#8230;</p>
<p><strong>Create a sample table without identity column:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_TestIdentity (ID INT, Name VARCHAR(10))

INSERT INTO tbl_TestIdentity VALUES 
(1,'ABC'), (2,'XYZ'),(3,'PQR')
,(4,'WER'),(5,'JKL'),(6,'ZXC')</pre> </p>
<p><strong>Check the data:</strong></p>
<pre class="crayon-plain-tag">SELECT *from tbl_TestIdentity

ID          Name
----------- ----------
1           ABC
2           XYZ
3           PQR
4           WER
5           JKL
6           ZXC</pre> </p>
<p><strong>Add new IDENTITY Column to table:</strong></p>
<pre class="crayon-plain-tag">ALTER TABLE tbl_TestIdentity ADD ID2 INT IDENTITY (1,1);</pre> </p>
<p><strong>Drop old ID Column:</strong></p>
<pre class="crayon-plain-tag">ALTER TABLE tbl_TestIdentity DROP COLUMN ID</pre> </p>
<p><strong>Rename new column to old column name:</strong></p>
<pre class="crayon-plain-tag">Exec sp_rename 'tbl_TestIdentity.ID2', 'ID', 'Column'</pre> </p>
<p><strong>Result:</strong></p>
<pre class="crayon-plain-tag">SELECT *from tbl_TestIdentity

Name       ID
---------- -----------
ABC        1
XYZ        2
PQR        3
WER        4
JKL        5
ZXC        6</pre> </p>
<blockquote class="wp-embedded-content" data-secret="kDApBclIAZ"><p><a href="https://www.dbrnd.com/2018/03/sql-server-how-to-remove-identity-from-a-column/">SQL Server: How to remove IDENTITY from a Column?</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2018/03/sql-server-how-to-remove-identity-from-a-column/embed/#?secret=kDApBclIAZ" data-secret="kDApBclIAZ" width="600" height="338" title="&#8220;SQL Server: How to remove IDENTITY from a Column?&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="SARIxB3LJX"><p><a href="https://www.dbrnd.com/2015/12/sql-server-script-to-find-identity-column-of-a-database/">SQL Server: Script to find Identity Column of a Database</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/12/sql-server-script-to-find-identity-column-of-a-database/embed/#?secret=SARIxB3LJX" data-secret="SARIxB3LJX" width="600" height="338" title="&#8220;SQL Server: Script to find Identity Column of a Database&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/01/sql-server-set-identity-to-existing-column-of-a-table/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
