{"id":123,"date":"2013-01-09T21:01:04","date_gmt":"2013-01-10T03:01:04","guid":{"rendered":"http:\/\/s1095387061.online-home.ca\/?p=123"},"modified":"2013-02-21T16:05:29","modified_gmt":"2013-02-21T22:05:29","slug":"qa-script-for-the-job-that-will-shrink-the-log-files-for-all-databases","status":"publish","type":"post","link":"https:\/\/databaserefresh.com\/blog\/qa-script-for-the-job-that-will-shrink-the-log-files-for-all-databases\/","title":{"rendered":"Q&#038;A: Script for the Job that will shrink the log files for all databases"},"content":{"rendered":"<p>There was a user question on how to shrink the log files with dynamic SQL (without hard coding databases names). The script is provided below. But first &#8211; make sure that you size the log files properly (large enough to grow) and make sure that you backup regularly the log files (or use simple recovery mode).  And do not truncate the logs, shrink them just to the size you need. So, if my log files do not usually grow larger than 4 GB I will use the following script:<\/p>\n<p><span style=\"color: #800000;\">SET NOCOUNT ON<\/span><br \/>\n<span style=\"color: #800000;\">CREATE   table #tran_log_space_usage( <\/span><br \/>\n<span style=\"color: #800000;\">        database_name sysname<\/span><br \/>\n<span style=\"color: #800000;\">,       log_size_mb float<\/span><br \/>\n<span style=\"color: #800000;\">,       log_space_used float<\/span><br \/>\n<span style=\"color: #800000;\">,       status int<\/span><br \/>\n<span style=\"color: #800000;\">); <\/span><br \/>\n<span style=\"color: #800000;\">insert into #tran_log_space_usage <\/span><br \/>\n<span style=\"color: #800000;\">exec(&#8216;DBCC SQLPERF ( LOGSPACE )&#8217;) ; <\/span><\/p>\n<p><span style=\"color: #800000;\">delete from #tran_log_space_usage where database_name = &#8216;Exclude_DB&#8217;<\/span><\/p>\n<p><span style=\"color: #800000;\">EXEC dbo.sp_msforeachdb &#8216;<\/span><br \/>\n<span style=\"color: #800000;\">DECLARE @log_file VARCHAR(150)<\/span><br \/>\n<span style=\"color: #800000;\">IF (SELECT DB_ID(&#8221;?&#8221;)) &gt; 4 and  (SELECT DATABASEPROPERTY(&#8221;?&#8221;, &#8221;IsOffline&#8221;)) &lt;&gt;1<\/span><br \/>\n<span style=\"color: #800000;\">BEGIN<\/span><br \/>\n<span style=\"color: #800000;\">    PRINT &#8221;?&#8221;<\/span><br \/>\n<span style=\"color: #800000;\">    IF (select log_size_mb as LogSizeMB<\/span><br \/>\n<span style=\"color: #800000;\">            from #tran_log_space_usage <\/span><br \/>\n<span style=\"color: #800000;\">            where database_name = &#8221;?&#8221;) &gt; 4048<\/span><br \/>\n<span style=\"color: #800000;\">        BEGIN<\/span><br \/>\n<span style=\"color: #800000;\">        select @log_file = name<\/span><br \/>\n<span style=\"color: #800000;\">            from [?].sys.database_files where file_id = 2<\/span><br \/>\n<span style=\"color: #800000;\">        USE [?]<\/span><br \/>\n<span style=\"color: #800000;\">            DBCC SHRINKFILE (@log_file , 4048) <\/span><br \/>\n<span style=\"color: #800000;\">        END<\/span><br \/>\n<span style=\"color: #800000;\">    PRINT &#8221;\/*********************************\/&#8221;<\/span><br \/>\n<span style=\"color: #800000;\">END&#8217;<\/span><\/p>\n<p><span style=\"color: #800000;\">DROP table #tran_log_space_usage<\/span><\/p>\n<p>You can add exceptions for the databases as well (see   &#8216;Exclude_DB&#8217; in script).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There was a user question on how to shrink the log files with dynamic SQL (without hard coding databases names). The script is provided below. But first &#8211; make sure that you size the log files properly (large enough to &hellip; <a href=\"https:\/\/databaserefresh.com\/blog\/qa-script-for-the-job-that-will-shrink-the-log-files-for-all-databases\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[7,31],"tags":[27,28,29],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-sql_maint","category-questions-answers","tag-qa","tag-question","tag-tips"],"_links":{"self":[{"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":8,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/posts\/123\/revisions\/127"}],"wp:attachment":[{"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}