Technical Article

Detach and Reattach all user databases

,

A step-by-step guide to help with server migration or SQL Server upgrades.

Debasis Sharma

/* Step 1 : Create a Database and Take the File Path */CREATE DATABASE [AttachAllDB] ON PRIMARY 
( NAME = N'AttachAllDB', FILENAME = N'<drive path>\AttachAllDB.mdf' , SIZE = 2048KB , FILEGROWTH = 1024KB )
LOG ON 
( NAME = N'AttachAllDB_log', FILENAME = N'<drive path>\AttachAllDB_log.ldf' , SIZE = 2048KB , FILEGROWTH = 10%)
GO
ALTER DATABASE [AttachAllDB] SET COMPATIBILITY_LEVEL = 100
GO
ALTER DATABASE [AttachAllDB] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [AttachAllDB] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [AttachAllDB] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [AttachAllDB] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [AttachAllDB] SET ARITHABORT OFF 
GO
ALTER DATABASE [AttachAllDB] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [AttachAllDB] SET AUTO_CREATE_STATISTICS ON 
GO
ALTER DATABASE [AttachAllDB] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [AttachAllDB] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [AttachAllDB] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [AttachAllDB] SET CURSOR_DEFAULT GLOBAL 
GO
ALTER DATABASE [AttachAllDB] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [AttachAllDB] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [AttachAllDB] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [AttachAllDB] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [AttachAllDB] SET DISABLE_BROKER 
GO
ALTER DATABASE [AttachAllDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [AttachAllDB] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [AttachAllDB] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [AttachAllDB] SET READ_WRITE 
GO
ALTER DATABASE [AttachAllDB] SET RECOVERY SIMPLE 
GO
ALTER DATABASE [AttachAllDB] SET MULTI_USER 
GO
ALTER DATABASE [AttachAllDB] SET PAGE_VERIFY CHECKSUM 
GO
USE [AttachAllDB]
GO
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY') ALTER DATABASE [AttachAllDB] MODIFY FILEGROUP [PRIMARY] DEFAULT
GO

/* End of Step 1 *//* Step 2 Create Table on AttachAllDB DB Insert the Data and Detach it */
CREATE TABLE AttachAllDBDetails 
(id bigint identity (1,1) primary key, 
DatabaseName nvarchar(500),
NameofFile nvarchar(500),
PhysicalFile nvarchar(500),
FileType nvarchar(50) )
INSERT INTO AttachAllDBDetails 
SELECT b.NAME AS DatabaseName, CAST(a.name AS varchar(500)) AS NameofFile, CAST(a.physical_name AS varchar(100)) AS PhysicalFile, 
 CASE WHEN a.type_desc = 'ROWS'
 THEN 'DataFile' 
 WHEN a.type_desc = 'LOG' THEN 'LogFile' ELSE '' END AS FileType
 
FROM sys.master_files A JOIN master.sys.databases B on A.database_id = B.database_id
WHERE (a.type_desc IN ('LOG', 'ROWS')) AND b.NAME NOT IN ('master','model','tempdb','msdb')

/* End of Step 2 */
/* Step 3 Detach all user databases */declare @dbname table (id bigint identity (1,1) primary key, dbname nvarchar(50))

insert into @dbname ( dbname)
select name from sys.sysdatabases where name not in ('master','model','tempdb','msdb')

declare @count int

set @count = (select COUNT(*) from @dbname)

while (@count <> 0)
begin

declare @db nvarchar(50)
declare @dblogquery nvarchar(2000)
declare @dblogtable table (dblogname nvarchar(50))
declare @dblog nvarchar(50)
declare @dblogshrinkquery nvarchar(2000)

set @db = (select top 1 dbname from @dbname)

EXEC master.dbo.sp_detach_db @db

delete from @dbname where dbname = @db
set @count = (select COUNT(*) from @dbname)

END
/* End of Step 3 *//* Step 4 Move all mdf and ldf to another server with same respective drive and directory
attach AttachAllDB the run bellow Setp 4 */USE [master]

CREATE TABLE #AttachDBDetails 
(id bigint identity (1,1) primary key, 
DatabaseName nvarchar(500),
NameofFile nvarchar(500),
PhysicalFile nvarchar(500),
FileType nvarchar(50) )
INSERT INTO #AttachDBDetails SELECT DatabaseName , NameofFile , PhysicalFile , FileType FROM AttachAllDB.dbo.AttachAllDBDetails 

--DROP TABLE #AttachDBDetails
declare @count int
set @count = (select COUNT(*) from #AttachDBDetails)
--PRINT @count

while (@count <> 0)
begin

declare @db nvarchar(50)
declare @attachdb nvarchar(max)
declare @dbdatafile nvarchar(500)
declare @dblogfile nvarchar(500)

SET @db = (SELECT DISTINCT top 1 DatabaseName FROM #AttachDBDetails)
SET @dbdatafile = ( SELECT PhysicalFile FROM #AttachDBDetails WHERE FileType = 'DataFile' AND DatabaseName = @db )
SET @dblogfile = ( SELECT PhysicalFile FROM #AttachDBDetails WHERE FileType = 'LogFile' AND DatabaseName = @db )
SET @attachdb = 'CREATE DATABASE ['+@db+'] ON 
( FILENAME = '''+@dbdatafile+'''),
( FILENAME = '''+@dblogfile+''' )
FOR ATTACH;
GO'
PRINT @attachdb
delete from #AttachDBDetails where DatabaseName = @db
set @count = (select COUNT(*) from #AttachDBDetails)

END
DROP TABLE #AttachDBDetails
/* End of Step 4 */

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating