>  기사  >  데이터 베이스  >  批量收缩数据库日志文件

批量收缩数据库日志文件

WBOY
WBOY원래의
2016-06-07 15:57:471185검색

declare @sql nvarchar(max),@paramDef nvarchar(100)declare dbname scroll cursor for select name,recovery_model from sys.databases where database_id7open dbnamedeclare @dbname varchar(50),@recovery_model int,@db_log_file varchar(100)set @dbn

declare @sql nvarchar(max),@paramDef nvarchar(100)
declare dbname scroll cursor for 
  select name,recovery_model from sys.databases where database_id>7
open dbname

declare @dbname varchar(50),@recovery_model int,@db_log_file varchar(100)
set @dbname=''
fetch first from dbname into @dbname,@recovery_model
while @@FETCH_STATUS=0
begin
  --alter recovery model
  if (@recovery_model<>3)
  begin
    set @sql=N&#39; alter database &#39;+@dbname+&#39; set RECOVERY  SIMPLE &#39;
    print &#39;Recovery :&#39;+@sql
   exec sp_executesql @sql
  end
  --get log file of database
  set @sql=N&#39;select @log_file=name from &#39;+@dbname+&#39;.sys.database_files where type=1&#39;
  set @paramDef= N&#39;@log_file varchar(100) OUTPUT &#39;
  exec sp_executesql @sql,@paramDef,@log_file=@db_log_file output
  print @db_log_file 
  
  --shrink file
  set @sql= N&#39;USE [&#39;+@dbname+&#39;] DBCC SHRINKFILE  (&#39;+@db_log_file+&#39;)&#39;
  exec sp_executesql @sql
  
  fetch next from dbname into @dbname,@recovery_model
end
close dbname
deallocate dbname
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.