Re: Renaming databases - code and gotchas
We are implmenting a rolling database history. We are going to keep 13 months of productiondb snapshots. At the end of each month db13 is renamed to db0, 12 to 13, 11 to 12, etc. And then at the end db0 is renamed to db1 and all db0 tables are truncated and then all tables are copied from productiondb to db1.
My code is below (obviously it needs some beefing up). But i have some questions and mostly want to know if anyone knows of any gotchas to look out for.
======= Questions =======================================
1) does the ALTER DATABASE products2DB SET SINGLE_USER WITH ROLLBACK IMMEDIATE terminate all connections? Is there a need to get the spid from the master..sysprocesses and KILL them?
2) technet says to "Back up the master database after you rename any database". Does anyone do this and is it necessary?
http://technet.microsoft.com/en-us/library/ms345378.aspx
3) Saw mention of running dbcc freeprocache after a rename to clear views. Does anyone do this and is it necessary?
"Run sp_refreshview or alternately run DBCC Freeprocache after database rename"
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/b66354e5-2c00-4396-a8db-a657d5062266/database-rename-problem-with-views-need-help
4) ALTER DATABASE RENAME gives the message "The file "products3db" has been modified in the system catalog. The new path will be used the next time the database is started." when rename. Does bringing the the database online "start" the database or is it looking for something else?
======= Notes - To do =======================================
1) code using the actual mdf and ldf names utilizing this query
SELECT *, name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'products2db')
2) find out about backing up the master
"Back up the master database after you rename any database"
http://technet.microsoft.com/en-us/library/ms345378.aspx
3) find out about running dbcc freeprocache after a rename
"Run sp_refreshview or alternately run DBCC Freeprocache after database rename"
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/b66354e5-2c00-4396-a8db-a657d5062266/database-rename-problem-with-views-need-help-
4) verify that async is off, or set it off if it is on
if on, set AUTO_UPDATE_STATISTICS_ASYNC OFF before setting single user - sys.databases
http://technet.microsoft.com/en-us/library/ms345598.aspx
select is_auto_update_stats_async_on, * from
sys.databases where name = 'products2db'
5) check is xp_cmdshell is on or off and set it back to what is was before my script
SELECT name AS [Configuration], CONVERT(INT, ISNULL(value, value_in_use)) AS [IsEnabled]
FROM master.sys.configurations
WHERE name = 'xp_cmdshell'
6) get message "The file "products3db" has been modified in the system catalog. The new path will be used the next time the database is started." when rename. Does bringing the the database online "start" the database?
7) does the ALTER DATABASE products2DB SET SINGLE_USER WITH ROLLBACK IMMEDIATE terminate all connections?
ALTER DATABASE products2DB SET SINGLE_USER WITH ROLLBACK IMMEDIATE ALTER DATABASE [products2DB] MODIFY FILE (NAME=N'products2DB', NEWNAME=N'products3DB') ALTER DATABASE [products2DB] MODIFY FILE (NAME=N'products2DB_log', NEWNAME=N'products3DB_log') ALTER DATABASE [products2DB] SET OFFLINE -- need to capture original setting and set back sp_configure 'xp_cmdshell', 1 GO RECONFIGURE WITH OVERRIDE GO -- need to get the file name dynamically EXEC xp_cmdshell 'RENAME "C:\temp\products2DB.mdf", "products3DB.mdf"' EXEC xp_cmdshell 'RENAME "C:\temp\products2DB_log.ldf", "products3DB_log.ldf"' ALTER DATABASE [products2DB] MODIFY FILE (NAME=N'products3DB', FILENAME=N'C:\TEMP\products3DB.mdf') ALTER DATABASE [products2DB] MODIFY FILE (NAME=N'products3DB_log', FILENAME=N'C:\TEMP\products3DB_log.ldf') ALTER DATABASE [products2DB] SET ONLINE ALTER DATABASE [products2DB] MODIFY Name = [products3DB] ALTER DATABASE products3DB SET MULTI_USER