Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
VinayBhargav
Starting Member
2 Posts |
Posted - 2012-08-01 : 18:31:21
|
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 isExecuted 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 ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[Shrink_Transactionlog]AS BEGINDECLARE @runid int,@rowcount int,@DBName varchar(1000),@LogName varchar(1000),@LogSize float,@PrcntUsed floatSET @runid = 5SET @rowcount = 0CREATE 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 + 1END/*Cleanup - drop the temp table*/drop table #logspaceENDVB |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
VinayBhargav
Starting Member
2 Posts |
Posted - 2012-08-01 : 19:02:40
|
I have already suggested the same, but I have been asked to do it. Now I have to complete this task.VB |
|
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2012-08-02 : 01:39:04
|
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 LOGSJack Vamvas--------------------http://www.sqlserver-dba.com |
|
|
Krishna_DBA
Starting Member
4 Posts |
Posted - 2012-08-02 : 10:37:17
|
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' |
|
|
|
|
|
|
|