How to move SQL Server databases to a new location by using Detach and Attach functions in SQL Server
This article was previously published under Q224071 On This PageSUMMARY This article describes how to change the location of the
data files and of the log files for any Microsoft SQL Server 2005, SQL Server
2000, or SQL Server 7.0 database. For more information about how to move system databases in SQL Server 2005, see the "Moving System Databases" topic in SQL Server Books Online. To view this topic, visit the following Microsoft Developer Network (MSDN) Web site: http://msdn2.microsoft.com/en-us/library/ms345408.aspx (http://msdn2.microsoft.com/en-us/library/ms345408.aspx) MORE INFORMATION The steps that you must follow to change the location for
some SQL Server system databases differ from the steps that you must follow to
change the location for user databases. These special cases are described
separately. Note SQL Server 7.0 system databases are not compatible with SQL Server 2000. Do not attach SQL Server 7.0 master, model, msdb or distribution databases to SQL Server 2000. If you are using SQL Server 2005, you can only attach databases of SQL Server 2005 to an instance. All the examples in this article assume that SQL Server is installed in the D:\Mssql7 folder. Additionally, the examples assume that all data files and log files are located in the default D:\Mssql7\Data folder. The examples move the data files and the log files for all the databases to the E:\Sqldata folder. Prerequisites
Moving user databasesThe following example moves a database that is named mydb. This database contains one data file, Mydb.mdf, and one log file, Mydblog.ldf. If the database that you are moving has more data files or log files, specify the files in a comma-delimited list in the sp_attach_db stored procedure. The sp_detach_db procedure does not change regardless of how many files the database contains because the sp_detach_db procedure does not list the files.
922804 (http://support.microsoft.com/kb/922804/)
FIX: After you detach a Microsoft SQL Server 2005 database that resides on network-attached storage, you cannot reattach the SQL Server database
Consider this issue. Additionally, consider the
permissions that are applied to a database when it is detached in SQL Server
2005. For more information, see the "Detaching and Attaching a Database"
section of the "Securing Data and Log Files" topic in SQL Server Books Online.
To view this topic, visit the following Microsoft Developer Network (MSDN) Web
site: http://msdn2.microsoft.com/en-us/library/ms189128.aspx (http://msdn2.microsoft.com/en-us/library/ms189128.aspx) Moving sample databasesTo move the pubs sample database and the Northwind sample database in SQL Server 2000 or in SQL Server 7.0, or to move the AdventureWorks sample database and the AdventureWorksDW sample database in SQL Server 2005, follow the same procedure for moving user databases.Moving the model databaseSQL Server 7.0
SQL Server 2005 and SQL Server 2000In SQL Server 2005 and in SQL Server 2000, you cannot detach system databases by using the sp_detach_db stored procedure. When you try to run the sp_detach_db 'model' statement, you receive the following error message:Server: Msg 7940, Level 16, State 1, Line 1 To
move the model database, you must start SQL Server together with the -c option, the -m option, and trace flag 3608. Trace flag 3608 prevents SQL Server
from recovering any database except the master database.System databases master, model, msdb, and tempdb cannot be detached. Note You will not be able to access any user databases after you do this. You must not perform any operations, other than the following steps, while you use this trace flag. To add trace flag 3608 as a SQL Server startup parameter, follow these steps:
http://msdn2.microsoft.com/en-us/library/ms190737.aspx (http://msdn2.microsoft.com/en-us/library/ms190737.aspx) After you add the -c option, the -m option, and trace flag 3608, follow these steps:
Moving the MSDB databaseSQL Server 7.0Note If you are using this procedure while moving the msdb and model databases, you must reattach the model database first, and then reattach the msdb database. Follow these steps:
Server: Msg 3702, Level 16, State 1,
Line 0 Cannot drop the database 'msdb' because it is currently in use. DBCC execution completed. If DBCC printed error messages, contact your system administrator. SQL Server 2005 and SQL Server 2000To move the MSDB database, you must start SQL Server together with the -c option, the -m option, and trace flag 3608. Trace flag 3608 prevents SQL Server from recovering any database except the master database. To add the -c option, the -m option, and trace flag 3608, follow the steps in the "Moving the model database" section. After you add the -c option, the -m option and trace flag 3608, follow these steps:
Msg 0, Level 11, State 0, Line 0
In this case, you must detach the msdb database, reattach the model database, and then reattach the msdb database, A severe error occurred on the current command. The results, if any, should be discarded. After you move the msdb database, you may receive the following error message: Error 229: EXECUTE permission denied on object
'ObjectName', database 'master', owner
'dbo'. This problem occurs because the ownership chain has been
broken. The database owners for the msdb database and for the master database are not the same. In this
case, the ownership of the msdb database had been changed. To work around this problem, run the
following Transact-SQL statements. You can do this by using the Osql.exe
command-line utility (SQL Server 7.0 and SQL Server 2000) or the Sqlcmd.exe
command-line utility (SQL Server 2005): For more information, click the following article number to
view the article in the Microsoft Knowledge Base: 272424 (http://support.microsoft.com/kb/272424/)
Object ownership chain checking across databases depends on the login
that is mapped to the object owners
Moving the master database
Moving the tempdb databaseYou can move tempdb files by using the ALTER DATABASE statement.
Moving the master database and the Resource databaseFor more information about moving the master database and the Resource database, visit the following Microsoft Developer Network (MSDN) Web site:http://msdn2.microsoft.com/en-us/library/ms345408.aspx (http://msdn2.microsoft.com/en-us/library/ms345408.aspx) REFERENCES
For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
912397 (http://support.microsoft.com/kb/912397/)
The SQL Server service cannot start when you change a startup parameter for a clustered instance of SQL Server 2000 or of SQL Server 2005 to a value that is not valid
274188 (http://support.microsoft.com/kb/274188/) "Troubleshooting orphaned users" topic in Books Online is incomplete
246133 (http://support.microsoft.com/kb/246133/) How to transfer logins and passwords between instances of SQL Server
168001 (http://support.microsoft.com/kb/168001/) User logons and permissions on a database may be incorrect after the database is restored
For more information, see the following books: Microsoft Corporation Microsoft SQL Server 7.0 System Administration Training Kit Microsoft Press, 2001 Microsoft Corporation MCSE Training Kit: Microsoft SQL Server 2000 System Administration (http://www.microsoft.com/mspress/books/4885.aspx) Microsoft Press, 2001 Microsoft Corporation Microsoft SQL Server 2000 Resource Kit (http://www.microsoft.com/mspress/books/4939.aspx) Microsoft Press, 2001 APPLIES TO
| Article Translations
|

Back to the top
