Created
April 22, 2022 14:27
-
-
Save JayHollingum/566d82c150ac300307f84811f38128e4 to your computer and use it in GitHub Desktop.
SQL Server - list db log files and generate script to switch to simple mode and shrink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select | |
db.name, | |
db.state_desc, | |
db.recovery_model_desc, | |
mf.type_desc, | |
mf.physical_name, | |
(mf.size * 8)/1024 as SizeMB, | |
'USE [master]; ALTER DATABASE [' + db.name + '] SET RECOVERY SIMPLE WITH NO_WAIT; USE [' + db.name + ']; DBCC SHRINKFILE (N''' + mf.name + ''' , 0, TRUNCATEONLY);' as SQLOutput | |
from | |
sys.databases db | |
inner join sys.master_files mf on db.database_id = mf.database_id | |
where | |
db.name not in ('master','tempdb','model','msdb') | |
and db.state_desc = 'online' | |
and db.recovery_model_desc = 'full' | |
and mf.type_desc = 'log' | |
order by | |
db.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment