netnerds.net

2Feb/070

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

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

Dirty Code

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

Posted by: Chrissy   Filed under: SQL Server Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.