T-SQL: Don't Run This in a Production Environment

Filed under: Quick Code, SQL Server — Written by Chrissy on Friday, February 2nd, 2007 @ 10:15 am

Here's some code that I'm posting mainly for my future reference. I do not recommend running this anywhere near a production machine.

DECLARE @SQL  varchar(8000) -- if you use nvarchar for whack table names, change this to 4000
SET @SQL = ''
SELECT @SQL = @SQL + 'ALTER DATABASE ' + NAME + ' SET RECOVERY SIMPLE; ALTER DATABASE ' + NAME + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DBCC SHRINKDATABASE (' + NAME + '); ALTER DATABASE ' + NAME + ' SET MULTI_USER' + Char(13)
FROM MASTER..Sysdatabases where sid != 0x01
 
PRINT @SQL
EXEC (@SQL)

This code does the following to all non-system databases:
1. Sets the recover to simple (my log files were OOC).
2. Kicks out all users and sets the db to single user
3. Shrinks the database files
4. Sets the database back to multi-user

  -

No comments yet. Be the first to comment this post.

Leave your comment