Friday, May 19, 2006

TSQL : Swap Databases

Swap databases code.

Useful for quickly switching content, e.g for websites.
ALTER DATABASE Database1  SET SINGLE_USER with rollback immediate
ALTER DATABASE Database2  SET SINGLE_USER with rollback immediate

EXEC sp_renamedb 'Database1', 'Database3'
EXEC sp_renamedb 'Database2', 'Database1'
EXEC sp_renamedb 'Database3', 'Database2'

ALTER DATABASE Database1  SET MULTI_USER
ALTER DATABASE Database2  SET MULTI_USER

No comments: