понедельник, 15 апреля 2024 г.

Shrink Log files

 declare @dbn varchar(max), @dbf varchar(max), @cmd varchar(max)

declare  CURS cursor for

SELECT 

    db.name AS [Database Name], 

    mf.name AS [Logical Name] 

FROM 

     sys.master_files AS mf

     INNER JOIN sys.databases AS db ON

            db.database_id = mf.database_id

where mf.type_desc = 'LOG' and db.database_id > 4;


open CURS;

FETCH NEXT FROM curs INTO @dbn, @dbf


WHILE @@FETCH_STATUS = 0

BEGIN

--print @dbn; print @dbf

select @cmd=''

select @cmd = 'use ['+@dbn+']; ALTER DATABASE ['+@dbn+'] MODIFY FILE (NAME = '''+@dbf+''', SIZE = 64, FILEGROWTH = 16MB); DBCC SHRINKFILE('''+@dbf+''',64);';

--print @cmd;

exec (@cmd);

FETCH NEXT FROM curs INTO @dbn, @dbf

end 

close CURS;

deallocate CURS;