I'm not looking to relocate the database to another server entirely, but just move the data file(s) and log file to another drive with more space. I've seen conflicting directions on how to do this, so I'm looking for the recommended proper way of doing it.
In SQL Server, you can move system and user databases by specifying the new file location in the FILENAME clause of the ALTER DATABASE statement. Data, log, and full-text catalog files can be moved in this way.
use master go sp_detach_db 'mydb'
DECLARE @SRCData nvarchar(1000) SET @SRCData = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\mydb.mdf'; DECLARE @SRCLog nvarchar(1000) SET @SRCLog = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\mydb_log.ldf'; DECLARE @FILEPATH nvarchar(1000); DECLARE @LOGPATH nvarchar(1000); SET @FILEPATH = N'xcopy /Y ' + @SRCData + N' D:\Data'; SET @LOGPATH = N'xcopy /Y ' + @SRCLog + N' E:\Log'; exec xp_cmdshell @FILEPATH; exec xp_cmdshell @LOGPATH;
sp_attach_db 'mydb', 'D:\Data\mydb.mdf', 'E:\Log\mydb_log.ldf'
There's more detail at this Microsoft KB article.
Another way - detach database files (database->tasks->detach), move them to new drive and then attach again. But way described by Jay S is the simpliest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With