Site Search:
Sign in | Join | Help
4Penny.net

SQL Server (T-SQL)

Comments and notes on SQL Server 2000, 2005, and T-SQL

Truncate the Log Size of All Databases on a SQL Server

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 and 2005

 

--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

 

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.