Asked By
kkorin
0 points
N/A
Posted on - 10/13/2011
Is it possible to create a new databases or move a database to an external drive? If so, how is it done, configuration wise?
Â
Create new SQL 2008 database in external drive
First things first.
You need to back-up your data, just in case the process goes wrong. Then, use the sp_detach_db and sp_attach_db system procedures to detach and then attach your databases.
Â
If users are trying to connect, it may help to set single user mode on the databases. After you detach the database with sp_detach_db you can move the files, then use sp_attach_db to attach them back to the SQL Server. If you want to move the system databases, it is a bit more tricky, involving setting a trace flag, moving the mdf and ldf files, stopping and starting the server, and finally changing the location of the master database. There are some gotchas possible in doing this.
Create new SQL 2008 database in external drive
Inside of Microsoft SQL Management Studio execute this script (replace "Name Of Database" with of the real database name):
Â
 USE master
 GO
 sp_detach_db 'NameOfDatabase'
 GO
Â
Copy the database at to the new location.
Â
 You should note that paths are must be relative to the server SQL which is running on.
Â
 USE master
 GO
 sp_attach_db 'NameOfDatabase', 'E:SQLServerDataFolderNameOfDatabase.mdf', 'E:SQLServerDataFolderNameOfDatabase_log.ldf'
 GO
Â
For moving the TempDB system database to the new location site do like this,
Â
USE master
 GO
 ALTER DATABASE tempdb modify file (name = tempdev, filename = 'E:SQLServerDataFoldertempdb.mdf')
 GO
 ALTER DATABASE tempdb modify file (name = templog, filename = 'E:SQLServerDataFoldertemplog.mdf')
Â
Lastly restart the SQL Server for the changes to take effect. Once it's restarted, you can delete the tempdb.mdf and templog.mdf from the old location.
Â
For more instructions visit the link.Â