This is is a handy piece of TSQL code which generates a list of TSQL commands that can be used to alter the location of all user databases to the path specified. It assumes that the filenames for the data file and log file will match the database name plus ".mdf" for the former and "_log.ldf" for the latter.
This can be executed while the SQL service is running and will only take effect after it has been restarted. You will of course have to physically copy the files to the new location when the service is down.
This can be executed while the SQL service is running and will only take effect after it has been restarted. You will of course have to physically copy the files to the new location when the service is down.
USE master;
GO
SELECT
'ALTER DATABASE [' +
name
+ ']
MODIFY FILE ( NAME = [' + name + '], FILENAME = ''F:\MSSQL\Data\' + name + '.mdf'' );
ALTER DATABASE [' +
name
+ ']
MODIFY FILE ( NAME = [' +
name +'_Log], FILENAME = ''E:\MSSQL\Log\' + name + '_Log.ldf'' );
GO
'
FROM sysdatabases
FROM sysdatabases
WHERE [sid] <> 0x01
ORDER BY name
Comments
Post a Comment