Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JayHollingum/566d82c150ac300307f84811f38128e4 to your computer and use it in GitHub Desktop.
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
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