- Blog - http://blog.bennett-scharf.com -

SQL Generators for moving database files

Posted By Bennett On 25. February 2010 @ 09:19 In T-SQL | 1 Comment

Here are several trivial scripts for updating the system catalog, moving files via robocopy, and adjusting filegrowth values. I hope someone finds them useful.

– generate system catalog changes
– *** note: need to manually edit destination path volume letter in output ***
SELECT ‘ALTER DATABASE ‘ + DB_NAME() + ‘ SET OFFLINE’

SELECT ‘ALTER DATABASE ‘ + DB_NAME() + ‘ MODIFY FILE
(
NAME = ‘
+ NAME + ‘,
FILENAME = N”’
+ filename + ”’
);’
FROM dbo.sysfiles
– where filename like ‘i:\%’
– order by size desc

SELECT ‘ALTER DATABASE ‘ + DB_NAME() + ‘ SET ONLINE’

– generate robocopy statements to be executed under command prompt
– *** note: need to manually edit destination path volume letter in output ***
SELECT ‘RoboCopy ‘ +
LEFT(filename,(LEN(filename) - CHARINDEX(‘\’, REVERSE(filename)))+1) + ‘ ‘ +
LEFT(filename,(LEN(filename) - CHARINDEX(‘\’, REVERSE(filename)))+1) + ‘ ‘ +
RIGHT(filename,(CHARINDEX(‘\’, REVERSE(filename)))-1)
FROM dbo.sysfiles

– modify filegrowth
SELECT ‘ALTER DATABASE ‘ + DB_NAME() + ‘ MODIFY FILE
(
NAME = ‘
+ NAME + ‘,
Filegrowth = 128MB
);’
FROM dbo.sysfiles


Article printed from Blog: http://blog.bennett-scharf.com

URL to article: http://blog.bennett-scharf.com/2010/02/25/sql-generators-for-moving-database-files/

Click here to print.