Skip to content

Instantly share code, notes, and snippets.

@Ogacha
Created December 27, 2017 07:43
Show Gist options
  • Save Ogacha/a86c113cce73dcf8673be8808fb97641 to your computer and use it in GitHub Desktop.
Save Ogacha/a86c113cce73dcf8673be8808fb97641 to your computer and use it in GitHub Desktop.
SQL Server 用 全DBのログ圧縮
create procedure dbo.ログ圧縮 as begin
-- 全DB のログ圧縮
declare @T table(SQL nvarchar(MAX));
insert @T select N'USE ['+name+'];
declare @log sysname;
select @log=name from sys.database_files where type=1;
DBCC SHRINKFILE (@log, 0, TRUNCATEONLY);'
from sys.databases where database_id>=7 and state=0;
declare SQL cursor for select * from @T;
declare @SQL nvarchar(MAX);
open SQL;
ループ:
fetch next from SQL into @SQL;
if @@FETCH_STATUS<>0 goto おしまい
print @SQL;
exec sp_executesql @SQL;
goto ループ;
おしまい:
close SQL;
deallocate SQL;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment