Criamos a stored procedure abaixo que automatiza o shrink …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
-- ================================================================================= -- Author: Eli Leiba -- Create date: 2020-03 -- Procedure Name: dbo.usp_ShrinkAllLogsExcludeSysDBS -- Description: This procedure shrinks all user databases log files or a specific user database log -- ================================================================================== CREATE PROCEDURE dbo.usp_ShrinkAllLogsExcludeSysDBS (@dbname SYSNAME = '%') AS BEGIN DECLARE @TSQLExec VARCHAR (MAX) = ''; SET NOCOUNT ON; IF OBJECT_ID('tempdb..#Temp') IS NOT NULL DROP TABLE #temp CREATE TABLE #temp (dbname sysname, dbid int, logFileSizeBeforeMB decimal(15,2), logFileSizeAfterMB decimal(15,2)); WITH fs AS ( SELECT database_id, TYPE, SIZE * 8.0 / 1024 SIZE FROM sys.master_files ) INSERT INTO #temp (dbname, dbid, logFileSizeBeforeMB) SELECT name, database_id, (SELECT SUM(SIZE) FROM fs WHERE TYPE = 1 AND fs.database_id = db.database_id) LogFileSizeMB FROM sys.databases db WHERE database_id > 4 AND NAME LIKE @dbname; SELECT @TSQLExec = CONCAT ( @TSQLExec, 'USE [', d.NAME, ']; CHECKPOINT; DBCC SHRINKFILE ([', f.NAME + ']) with no_infomsgs;' , Char (13),Char (10)) FROM sys.databases d, sys.master_files f WHERE d.database_id = f.database_id AND d.database_id > 4 AND f.type = 1 AND d.NAME LIKE @dbname; PRINT @TSQLExec; EXEC (@TSQLExec); WITH fs AS ( SELECT database_id, TYPE, SIZE * 8.0 / 1024 SIZE FROM sys.master_files ) UPDATE a set a.logFileSizeAfterMB = (SELECT SUM(SIZE) FROM fs WHERE TYPE = 1 AND fs.database_id = db.database_id) FROM #temp a inner join sys.databases db on a.dbid = db.database_id WHERE database_id > 4 AND NAME LIKE @dbname SELECT * FROM #temp ORDER BY dbname SET NOCOUNT OFF; END; GO |
Depois rodamos o comando abaixo ou podemos também colocar em um Job.
1 2 3 4 |
USE master GO EXEC dbo.usp_ShrinkAllLogsExcludeSysDBS GO |
Fonte: https://www.mssqltips.com/sqlservertip/6385/shrink-sql-server-transaction-log-for-all-databases/