A noter qu'à partir de ... SQL 2008 ? Ou 2012, la commande BACKUP LOG [NomDeLaBase] WITH TRUNCATE_ONLY ne fonctionne plus. En alternative il faut passer la base en mode de recouvrement simple.
DECLARE @DBName varchar(255)
DECLARE @DBLogicalFileName varchar(255)
DECLARE @DATABASES_Fetch int
DECLARE DATABASES_CURSOR CURSOR FOR
select DATABASE_NAME = db_name(MaTableMasterFiles.database_id), MaTableMasterFiles.name
from sys.master_files MaTableMasterFiles
where
-- ONLINE
MaTableMasterFiles.state = 0
-- Only look at databases to which we have access
and has_dbaccess(db_name(MaTableMasterFiles.database_id)) = 1
-- Not master, tempdb or model
and db_name(MaTableMasterFiles.database_id) not in ('Master','tempdb','model')
AND type_desc LIKE 'log'
group by MaTableMasterFiles.database_id, MaTableMasterFiles.name
order by 1
OPEN DATABASES_CURSOR
FETCH NEXT FROM DATABASES_CURSOR INTO @DBName, @DBLogicalFileName
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Process Database ' + @DBName
exec ('BACKUP LOG [' + @DBName + '] WITH TRUNCATE_ONLY')
exec ('Use [' + @DBName + '] DBCC SHRINKFILE ("' + @DBLogicalFileName + '")')
FETCH NEXT FROM DATABASES_CURSOR INTO @DBName, @DBLogicalFileName
END
CLOSE DATABASES_CURSOR
DEALLOCATE DATABASES_CURSOR
Lien vers le fichier : cliquez ici
Article(s) précédent(s)