<?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>attach &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/attach/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>Fri, 08 Feb 2019 18:43:57 +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: 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: Move your database using Attach and Detach</title>
		<link>https://www.dbrnd.com/2017/05/sql-server-move-your-database-using-attach-and-detach-copy-database/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 19 May 2017 16:16:33 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[attach]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[detach]]></category>
		<category><![CDATA[Move Database]]></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=5087</guid>

					<description><![CDATA[In this post, I am going to share necessary steps to move your database from one server to another using Attach and Detach. There are multiple ways for this exercise like using database backup file, using COPY command, moving data files and log files and others. But if you want to move your database completely, [&#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%2F2017%2F05%2Fsql-server-move-your-database-using-attach-and-detach-copy-database%2F&amp;linkname=SQL%20Server%3A%20Move%20your%20database%20using%20Attach%20and%20Detach" 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%2F2017%2F05%2Fsql-server-move-your-database-using-attach-and-detach-copy-database%2F&amp;linkname=SQL%20Server%3A%20Move%20your%20database%20using%20Attach%20and%20Detach" 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%2F2017%2F05%2Fsql-server-move-your-database-using-attach-and-detach-copy-database%2F&amp;linkname=SQL%20Server%3A%20Move%20your%20database%20using%20Attach%20and%20Detach" 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%2F2017%2F05%2Fsql-server-move-your-database-using-attach-and-detach-copy-database%2F&amp;linkname=SQL%20Server%3A%20Move%20your%20database%20using%20Attach%20and%20Detach" 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%2F2017%2F05%2Fsql-server-move-your-database-using-attach-and-detach-copy-database%2F&amp;linkname=SQL%20Server%3A%20Move%20your%20database%20using%20Attach%20and%20Detach" 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%2F2017%2F05%2Fsql-server-move-your-database-using-attach-and-detach-copy-database%2F&amp;linkname=SQL%20Server%3A%20Move%20your%20database%20using%20Attach%20and%20Detach" 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%2F2017%2F05%2Fsql-server-move-your-database-using-attach-and-detach-copy-database%2F&#038;title=SQL%20Server%3A%20Move%20your%20database%20using%20Attach%20and%20Detach" data-a2a-url="https://www.dbrnd.com/2017/05/sql-server-move-your-database-using-attach-and-detach-copy-database/" data-a2a-title="SQL Server: Move your database using Attach and Detach"></a></p><p>In this post, I am going to share necessary steps to move your database from one server to another using Attach and Detach.<br />
There are multiple ways for this exercise like using database backup file, using COPY command, moving data files and log files and others.</p>
<p>But if you want to move your database completely, you can use Attach and Detach in SQL Server.<br />
What you need to do is, just detach your database, copy the database files, paste those files in your new location and attach it in your destination location. </p>
<p><strong>Step 1:</strong></p>
<pre class="crayon-plain-tag">T-SQL Script to detach your database:
USE master;  
GO  
EXEC sp_detach_db @dbname = 'my_db';  
GO</pre> </p>
<p><strong>Step 2:</strong></p>
<blockquote><p> Copy the database files (.mdf,.ndf,.ldf) and paste into new location. Also, check the windows permission for those files.</p></blockquote>
<p><strong>Step 3:</strong><br />
T-SQL Script to attach your database: </p>
<pre class="crayon-plain-tag">USE master
GO  
CREATE DATABASE my_db   
    ON (FILENAME = 'C:\MySQL_Data\my_db_data.mdf'),  
    (FILENAME = 'C:\MySQL_Data\my_db_log.ldf')  
    FOR ATTACH
GO</pre> </p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
