Feb
18
Written by:
Steve Gray
2/18/2010 10:20 AM
This is a combination of logic from 2 previous posts - this piece of code will truncate the log file size for all the databases in a SQL Server. It has been tested against SQL 2000, 2005 and 2008
--delcare variables
declare @vchrTable varchar(200),
@vchrMsg varchar(200)
--declare the cursor
DECLARE curTables CURSOR FOR
SELECT name from master..sysdatabases order by 1
--open the cursor
OPEN curTables
--loop through the cursor lines
FETCH NEXT FROM curTables INTO @vchrTable
WHILE @@FETCH_STATUS = 0
BEGIN
set @vchrMsg =
'DBCC SHRINKDATABASE ( [' + @vchrTable + '] ,10);'
print @vchrMsg
exec (@vchrMsg)
set @vchrMsg =
'backup log [' + @vchrTable + '] with truncate_only;'
print @vchrMsg
exec (@vchrMsg)
--backup log tstoday with truncate_only
FETCH NEXT FROM curTables INTO @vchrTable
END
--clean up
CLOSE curTables
DEALLOCATE curTables
As always, I welcome your comments!