How to handle multiple .MDF files and .LDF files in SQL Server 2008

August 25, 2009

Part 1 of this article series illustrated the use of the "Create Database" statement "For Attach" and "for ATTACH_REBUILD_LOG" for a single .MDF file and single .LDF file. This installment illustrates how to handle multiple .MDF files and .LDF files.

Let's assume that we have a database, MyDB1, with two .MDF files and two .LDF files as shown below. Execute the following Transact SQL Statement.

/****** Object:  Database [MyDB1]    
 Script Date: 08/11/2009 11:24:25 ******/
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'MyDB1')
DROP DATABASE [MyDB1]
GO

CREATE DATABASE MyDB1
ON 
( NAME = MyDB1_dat1,FILENAME = 'C:\
 Program Files\Microsoft SQL Server\
 MSSQL10.SQL2008\MSSQL\DATA\MyDB1.mdf',SIZE = 2),
( NAME = MyDB1_dat2,FILENAME = 'C:\
 Program Files\Microsoft SQL Server\
 MSSQL10.SQL2008\MSSQL\DATA\MyDB2.mdf',SIZE = 2)
LOG ON
( NAME = MyDB1_log1,FILENAME = 'C:\
 Program Files\Microsoft SQL Server
 \MSSQL10.SQL2008\MSSQL\DATA\MyDB1.ldf', SIZE = 2MB ),
( NAME = MyDB1_log2,FILENAME = 'C:\
 Program Files\Microsoft SQL Server
 \MSSQL10.SQL2008\MSSQL\DATA\MyDB2.ldf', SIZE = 2MB )
GO

Now let's detach the MyDB1 database and reattach it using sp_attach_db.

use master
go
sp_detach_db 'MyDB1'
go

sp_attach_db 'MyDb1',
'C:\Program Files\Microsoft SQL Server\
 MSSQL10.SQL2008\MSSQL\DATA\MyDB1.mdf',
'C:\Program Files\Microsoft SQL Server\
 MSSQL10.SQL2008\MSSQL\DATA\MyDB2.mdf',
'C:\Program Files\Microsoft SQL Server\
 MSSQL10.SQL2008\MSSQL\DATA\MyDB1.ldf',
'C:\Program Files\Microsoft SQL Server\
 MSSQL10.SQL2008\MSSQL\DATA\MyDB2.ldf'
go

The same files could be attached using "Create database" with the "For Attach" transact SQL statement as shown below.

use master
go
sp_detach_db 'MyDB1'
go

CREATE DATABASE MyDB1
ON 
(FILENAME = 'C:\Program Files\Microsoft SQL Server\
 MSSQL10.SQL2008\MSSQL\DATA\MyDB1.mdf'), 
(FILENAME = 'C:\Program Files\Microsoft SQL Server\
 MSSQL10.SQL2008\MSSQL\DATA\MyDB2.mdf'), 
(FILENAME = 'C:\Program Files\Microsoft SQL Server\
 MSSQL10.SQL2008\MSSQL\DATA\MyDB1.ldf'), 
(FILENAME = 'C:\Program Files\Microsoft SQL Server\
 MSSQL10.SQL2008\MSSQL\DATA\MyDB1.ldf') 
for Attach

Now let's assume that we have the MyDB1 database, with more than sixteen .MDF files and more than sixteen .LDF files, as shown below. Execute the following Transact SQL Statement.

/****** Object:  Database [MyDB1]    Script Date: 08/11/2009 11:24:25 ******/
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'MyDB1')
DROP DATABASE [MyDB1]
GO

CREATE DATABASE MyDB1
ON 
( NAME = MyDB1_dat1,FILENAME = 'C:\Data\MyDB1.mdf',SIZE = 2),
( NAME = MyDB1_dat2,FILENAME = 'C:\Data\MyDB2.mdf',SIZE = 2),
( NAME = MyDB1_dat3,FILENAME = 'C:\Data\MyDB3.mdf',SIZE = 2),
( NAME = MyDB1_dat4,FILENAME = 'C:\Data\MyDB4.mdf',SIZE = 2),
( NAME = MyDB1_dat5,FILENAME = 'C:\Data\MyDB5.mdf',SIZE = 2),
( NAME = MyDB1_dat6,FILENAME = 'C:\Data\MyDB6.mdf',SIZE = 2),
( NAME = MyDB1_dat7,FILENAME = 'C:\Data\MyDB7.mdf',SIZE = 2),
( NAME = MyDB1_dat8,FILENAME = 'C:\Data\MyDB8.mdf',SIZE = 2),
( NAME = MyDB1_dat9,FILENAME = 'C:\Data\MyDB9.mdf',SIZE = 2),
( NAME = MyDB1_dat10,FILENAME = 'C:\Data\MyDB10.mdf',SIZE = 2),
( NAME = MyDB1_dat11,FILENAME = 'C:\Data\MyDB11.mdf',SIZE = 2),
( NAME = MyDB1_dat12,FILENAME = 'C:\Data\MyDB12.mdf',SIZE = 2),
( NAME = MyDB1_dat13,FILENAME = 'C:\Data\MyDB13.mdf',SIZE = 2),
( NAME = MyDB1_dat14,FILENAME = 'C:\Data\MyDB14.mdf',SIZE = 2),
( NAME = MyDB1_dat15,FILENAME = 'C:\Data\MyDB15.mdf',SIZE = 2),
( NAME = MyDB1_dat16,FILENAME = 'C:\Data\MyDB16.mdf',SIZE = 2),
( NAME = MyDB1_dat17,FILENAME = 'C:\Data\MyDB17.mdf',SIZE = 2),
( NAME = MyDB1_dat18,FILENAME = 'C:\Data\MyDB18.mdf',SIZE = 2)
LOG ON
( NAME = MyDB1_log1,FILENAME = 'C:\Data\MyDB1.ldf', SIZE = 2MB ),
( NAME = MyDB1_log2,FILENAME = 'C:\Data\MyDB2.ldf', SIZE = 2MB ),
( NAME = MyDB1_log3,FILENAME = 'C:\Data\MyDB3.ldf', SIZE = 2MB ),
( NAME = MyDB1_log4,FILENAME = 'C:\Data\MyDB4.ldf', SIZE = 2MB ),
( NAME = MyDB1_log5,FILENAME = 'C:\Data\MyDB5.ldf', SIZE = 2MB ),
( NAME = MyDB1_log6,FILENAME = 'C:\Data\MyDB6.ldf', SIZE = 2MB ),
( NAME = MyDB1_log7,FILENAME = 'C:\Data\MyDB7.ldf', SIZE = 2MB ),
( NAME = MyDB1_log8,FILENAME = 'C:\Data\MyDB8.ldf', SIZE = 2MB ),
( NAME = MyDB1_log9,FILENAME = 'C:\Data\MyDB9.ldf', SIZE = 2MB ),
( NAME = MyDB1_log10,FILENAME = 'C:\Data\MyDB10.ldf', SIZE = 2MB ),
( NAME = MyDB1_log11,FILENAME = 'C:\Data\MyDB11.ldf', SIZE = 2MB ),
( NAME = MyDB1_log12,FILENAME = 'C:\Data\MyDB12.ldf', SIZE = 2MB ),
( NAME = MyDB1_log13,FILENAME = 'C:\Data\MyDB13.ldf', SIZE = 2MB ),
( NAME = MyDB1_log14,FILENAME = 'C:\Data\MyDB14.ldf', SIZE = 2MB ),
( NAME = MyDB1_log15,FILENAME = 'C:\Data\MyDB15.ldf', SIZE = 2MB ),
( NAME = MyDB1_log16,FILENAME = 'C:\Data\MyDB16.ldf', SIZE = 2MB ),
( NAME = MyDB1_log17,FILENAME = 'C:\Data\MyDB17.ldf', SIZE = 2MB )
GO

Now let's detach this database and reattach it using sp_attach_db.

use master
go
sp_detach_db 'MyDB1'
go

sp_attach_db 'MyDb1','C:\DATA\MyDB1.mdf',
'C:\DATA\MyDB2.mdf','C:\DATA\MyDB3.mdf','C:\DATA\MyDB4.mdf',
'C:\DATA\MyDB5.mdf','C:\DATA\MyDB6.mdf','C:\DATA\MyDB7.mdf',
'C:\DATA\MyDB8.mdf','C:\DATA\MyDB9.mdf','C:\DATA\MyDB10.mdf',
'C:\DATA\MyDB11.mdf','C:\DATA\MyDB12.mdf','C:\DATA\MyDB13.mdf',
'C:\DATA\MyDB14.mdf','C:\DATA\MyDB15.mdf','C:\DATA\MyDB16.mdf',
'C:\DATA\MyDB17.mdf','C:\DATA\MyDB18.mdf','C:\DATA\MyDB1.ldf',
'C:\DATA\MyDB2.ldf',
'C:\DATA\MyDB3.ldf','C:\DATA\MyDB4.ldf','C:\DATA\MyDB5.ldf'
,'C:\DATA\MyDB6.ldf','C:\DATA\MyDB7.ldf','C:\DATA\MyDB8.ldf',
'C:\DATA\MyDB9.ldf','C:\DATA\MyDB10.ldf','C:\DATA\MyDB11.ldf'
,'C:\DATA\MyDB12.ldf','C:\DATA\MyDB13.ldf','C:\DATA\MyDB14.ldf',
'C:\DATA\MyDB15.ldf','C:\DATA\MyDB16.ldf','C:\DATA\MyDB17.ldf'
go

Result:

Msg 8144, Level 16, State 2, Procedure sp_attach_db, Line 0
Procedure or function sp_attach_db has too many arguments specified.

sp_attach_db cannot handle more than 16 files. It accepts a maximum of 16 parameters.

Let's try to attach all of these files using the "Create Database" statement "For Attach".

use master
go

CREATE DATABASE MyDB1
ON 
(FILENAME = 'C:\Data\MyDB1.mdf'),(FILENAME = 'C:\Data\MyDB2.mdf'),
(FILENAME = 'C:\Data\MyDB3.mdf'),(FILENAME = 'C:\Data\MyDB4.mdf'),
(FILENAME = 'C:\Data\MyDB5.mdf'),(FILENAME = 'C:\Data\MyDB6.mdf'),
(FILENAME = 'C:\Data\MyDB7.mdf'),(FILENAME = 'C:\Data\MyDB8.mdf'),
(FILENAME = 'C:\Data\MyDB9.mdf'),(FILENAME = 'C:\Data\MyDB10.mdf'),
(FILENAME = 'C:\Data\MyDB11.mdf'),(FILENAME = 'C:\Data\MyDB12.mdf'),
(FILENAME = 'C:\Data\MyDB13.mdf'),(FILENAME = 'C:\Data\MyDB14.mdf'),
(FILENAME = 'C:\Data\MyDB15.mdf'),(FILENAME = 'C:\Data\MyDB16.mdf'),
(FILENAME = 'C:\Data\MyDB17.mdf'),(FILENAME = 'C:\Data\MyDB18.mdf')
LOG ON
( FILENAME = 'C:\Data\MyDB1.ldf'),( FILENAME = 'C:\Data\MyDB2.ldf'),
( FILENAME = 'C:\Data\MyDB3.ldf'),( FILENAME = 'C:\Data\MyDB4.ldf'),
( FILENAME = 'C:\Data\MyDB5.ldf'),( FILENAME = 'C:\Data\MyDB6.ldf'),
( FILENAME = 'C:\Data\MyDB7.ldf'),( FILENAME = 'C:\Data\MyDB8.ldf'),
( FILENAME = 'C:\Data\MyDB9.ldf'),( FILENAME = 'C:\Data\MyDB10.ldf'),
( FILENAME = 'C:\Data\MyDB11.ldf'),( FILENAME = 'C:\Data\MyDB12.ldf'),
( FILENAME = 'C:\Data\MyDB13.ldf'),( FILENAME = 'C:\Data\MyDB14.ldf'),
( FILENAME = 'C:\Data\MyDB15.ldf'),( FILENAME = 'C:\Data\MyDB16.ldf'),
( FILENAME = 'C:\Data\MyDB17.ldf')
for attach

Conclusion

This installment illustrated how to handle multiple .MDF files and .LDF files. The next part of this article series is going to illustrate how to rebuild the log for multiple .MDF files.

» See All Articles by Columnist MAK








The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers