Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Tranaction Logs on a Test Box Expand / Collapse
Author
Message
Posted Friday, March 28, 2014 8:15 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, November 7, 2014 2:00 PM
Points: 979, Visits: 3,364
Hello Everyone, and Happy Friday!
I am doing some general housekeeping for a couple of our SQL boxes in the Development environment. All the databases are set to Simple recovery mode. No need in anything else for these boxes. I have a database on all the boxes named "DatabaseMaintenance" Keeps things like all the sprocs for any type of database maintenance, etc....

I would like to schedule a single sproc that is located in the DatabaseMaintenance database to shrink the Transaction logs on a set schedule. They sometimes grow quite large while testing and developing. The thing that I cannot seem to get around, is when using the ShrinkFile command, one must use the Log Name. If this code is in a sproc that is located in the DatabaseMaintenance database, it will fail when attempting to call out to a different database. Because the Log does not exist on the database that the sproc is located.

How can I get around this small dilemma? There are only about 10 databases per box. To a point we really do not care what happens to them. They are on a Full backup schedule daily, just to keep the objects. As I stated previously, the logs will still grow huge at times while pumping data.

Is there a way to create a piece of code that will run against each database on the server, and be stored in a single database? Other than the system databases of course.

Thank you in advance for all your assistance, suggestions and comments.

Andrew SQLDBA
Post #1555938
Posted Monday, March 31, 2014 10:19 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:42 AM
Points: 1,309, Visits: 1,573
Hi Andrew,

I have created this for you from a similar maintenance task I undertake:

EXEC sp_MSforeachdb 'IF ''?''  NOT IN (''tempDB'',''model'',''msdb'', ''master'')
BEGIN
DECLARE @desiredSizeInMB INT = 16, @execSQL NVARCHAR(MAX)
SELECT @execSQL =
''DBCC SHRINKFILE ('' + NAME + '' ,'' + CAST(@desiredSizeInMB AS VARCHAR(4)) + '' ) WITH NO_INFOMSGS;''
FROM
[?].[sys].[database_files] AS DF
WHERE
[type] = 1

use [?];
EXEC sp_executesql @execSQL

END'

Edited - actually execute the SQL!



_____________________________________________________________________________________
gsc_dba
Post #1556579
Posted Monday, March 31, 2014 10:30 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:09 AM
Points: 12,962, Visits: 32,500
Andrew in my shop, all databases on development boxes are switched over to simple recovery mode; i don't need a point in time restore on development that log backups would provide me; would it be an option to change them to simple recovery and shrink the logs once, isntead of letting them remain in full recovery?

Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1556587
Posted Monday, March 31, 2014 11:30 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, November 7, 2014 2:00 PM
Points: 979, Visits: 3,364
Hey Lowell
Yes, in fact they are already in Simple recovery mode. But with the amount of data being pumped int at times, they can grow. And over time, they get big. Not large, just big, and I was asked to do what I can do to save as much drive space as possible. On this box, 10 - 15GB is a nice amount of drive space.

Thanks for your suggestions, comments and answer to my question

Andrew SQLDBA
Post #1556620
Posted Monday, March 31, 2014 11:33 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, November 7, 2014 2:00 PM
Points: 979, Visits: 3,364
Thanks GSC_DBA
That is really Great!! I tried using MS_ForEachDatabase, but could not get it working correctly. I was missing something in the dynamic code, and could not get it correct.

I will give this a try and see if this works. It looks better than what I have written :)

I greatly appreciate your time, code, comment and suggestion.

Andrew SQLDBA
Post #1556621
Posted Monday, March 31, 2014 3:19 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 1:14 PM
Points: 40,615, Visits: 37,081
Or, as something a tad easier:

  exec sp_MSforeachDB 'USE [?]; IF ''?'' NOT in (''master'',''model'',''msdb'',''tempDB'') DBCC ShrinkFile(2,10)'

Untested, so may have syntax errors. Test before use.
Shrinkfile takes either the file name or the file ID as the first parameter, and the first log file is always file ID 2 (and if you have more than one log file, that needs fixing)



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1556736
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse