SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 Shrink Transaction Logs on a Weekly basis
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

VinayBhargav
Starting Member

USA
2 Posts

Posted - 08/01/2012 :  18:31:21  Show Profile  Reply with Quote
Hello All,

I have a task at hand to shrink transaction logs on a weekly basis. I have created the below pasted stored procedure to achieve it. The logic seems ok to me, but let me know if I am making mistake anywhere.

The error i am facing is

Executed as user: NT AUTHORITY\SYSTEM. DBCC execution completed. If DBCC printed error messages, contact your system administrator. [SQLSTATE 01000] (Message 2528) Could not locate file 'PRC_Data_Warehouse_log' for database 'tempdb'. [SQLSTATE 42000] (Error 8985). The step failed.

I need to pass Database name dynamically to achieve this task. Please provide your valuable suggestions.


USE [master]
GO
/****** Object: StoredProcedure [dbo].[Shrink_Transactionlog] Script Date: 08/01/2012 15:24:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Shrink_Transactionlog]
AS
BEGIN

DECLARE @runid int,
@rowcount int,
@DBName varchar(1000),
@LogName varchar(1000),
@LogSize float,
@PrcntUsed float

SET @runid = 5
SET @rowcount = 0

CREATE TABLE #logspace (
DBName varchar( 100),
LogSize float,
PrcntUsed float,
status int,
CountCheck INT IDENTITY(1,1)
)
INSERT INTO #logspace
EXEC ('DBCC sqlperf(logspace)')
/*
process the data
*/
--ALTER TABLE #logspace ADD CountCheck INT IDENTITY(1,1)

SET @rowcount = (select COUNT(*) from #logspace)

While (@runid <= @rowcount)

Begin

SET @DBName = (SELECT DBName from #logspace
WHERE #logspace.CountCheck = @runid)

SET @LogName = (Select F.NAME
FROM MASTER.SYS.MASTER_FILES F
INNER JOIN MASTER.SYS.DATABASES DB
ON DB.database_id = F.database_id
WHERE F.file_id = 2
AND DB.name = @DBName)

SET @LogSize = (select LogSize from #logspace where DBName = @DBName)

SET @PrcntUsed = (select PrcntUsed from #logspace where DBName = @DBName)

IF @LogName is not NULL
BEGIN
IF (@LogSize > 100.00)
Begin
DBCC SHRINKFILE (@LogName, 10) WITH NO_INFOMSGS
SET @runid = @runid + 1
End
ELSE
IF (@PrcntUsed > 50.00)
Begin
DBCC SHRINKFILE (@LogName, 10) WITH NO_INFOMSGS
SET @runid = @runid + 1
End
Else
SET @runid = @runid + 1
END
ELSE
SET @runid = @runid + 1

END
/*
Cleanup - drop the temp table
*/

drop table #logspace

END

VB

tkizer
Almighty SQL Goddess

USA
35007 Posts

Posted - 08/01/2012 :  18:51:19  Show Profile  Visit tkizer's Homepage  Reply with Quote
You should not be shrinking the transaction logs on a scheduled basis!

Here are some articles:
http://www.sqlskills.com/blogs/paul/post/why-you-should-not-shrink-your-data-files.aspx
http://www.sqlservercentral.com/articles/Administration/64582/

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

VinayBhargav
Starting Member

USA
2 Posts

Posted - 08/01/2012 :  19:02:40  Show Profile  Reply with Quote
I have already suggested the same, but I have been asked to do it. Now I have to complete this task.

VB
Go to Top of Page

jackv
Flowing Fount of Yak Knowledge

United Kingdom
1769 Posts

Posted - 08/02/2012 :  01:39:04  Show Profile  Visit jackv's Homepage  Reply with Quote
Analyse firstly , why the log files are growing to a certain size. If the transaction log files are growing to a certain size, then keep the size.
To keep the transaction log files smaller , commit some regular BACKUP LOGS

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

Krishna_DBA
Starting Member

4 Posts

Posted - 08/02/2012 :  10:37:17  Show Profile  Reply with Quote
As tkizer and Jack suggested, you should not be shrinking the logs. However if you have to do it, then get the DBCC command to a variable and execute it as below.

set @sql = 'use ' + @DBName ';DBCC SHRINKFILE (' + @LogName + ', 10) WITH NO_INFOMSGS'
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000