-------------------------------循环数据库压缩日志文件---------------------------
2013-01-05 20:32
543 查看
--循环数据库压缩日志文件(假设日志文件fileid=2)
create table #db(id int identity(1,1),name varchar(80),dbsize int,mdfsize int,ldfsize int)
insert into #db
select a.name,SUM(b.size)*8/1024 as 'dbsize[MB]',
SUM(case when b.type<>1 then b.size else 0 end)*8/1024 as 'datafilesize[MB]',
SUM(case when b.type=1 then b.size else 0 end)*8/1024 as 'logfilesize[MB]'
from sys.databases as a
inner join sys.master_files as b on a.database_id=b.database_id
where a.database_id>4
group by a.name
order by SUM(case when b.type=1 then b.size else 0 end)--按日志从大到小排序
declare @min_id int,@max_id int,@tmp_db varchar(80),@s varchar(max)
select @min_id=min(id),@max_id=max(id) from #db where ldfsize>0--此处指定日志文件大于多少M的
while @min_id<=@max_id
begin
select @tmp_db=name,@min_id=@min_id+1 from #db where id=@min_id
set @s='BACKUP LOG '+@tmp_db+' with no_log'
exec(@s)
set @s='use '+@tmp_db+';dbcc shrinkfile(2)'
exec(@s)
end
drop table #db
create table #db(id int identity(1,1),name varchar(80),dbsize int,mdfsize int,ldfsize int)
insert into #db
select a.name,SUM(b.size)*8/1024 as 'dbsize[MB]',
SUM(case when b.type<>1 then b.size else 0 end)*8/1024 as 'datafilesize[MB]',
SUM(case when b.type=1 then b.size else 0 end)*8/1024 as 'logfilesize[MB]'
from sys.databases as a
inner join sys.master_files as b on a.database_id=b.database_id
where a.database_id>4
group by a.name
order by SUM(case when b.type=1 then b.size else 0 end)--按日志从大到小排序
declare @min_id int,@max_id int,@tmp_db varchar(80),@s varchar(max)
select @min_id=min(id),@max_id=max(id) from #db where ldfsize>0--此处指定日志文件大于多少M的
while @min_id<=@max_id
begin
select @tmp_db=name,@min_id=@min_id+1 from #db where id=@min_id
set @s='BACKUP LOG '+@tmp_db+' with no_log'
exec(@s)
set @s='use '+@tmp_db+';dbcc shrinkfile(2)'
exec(@s)
end
drop table #db
相关文章推荐
- sql server 2005 压缩实例下所有数据库文件及日志文件 语句(转)
- SQL Server压缩日志及数据库文件大小
- SQL2005 压缩日志及数据库文件大小
- 压缩日志及数据库文件大小
- 压缩日志及数据库文件大小(ms SQL SERVER)
- 压缩日志及数据库文件大小
- sql server 2008数据库日志文件压缩截断方法
- Sql Server 2008 压缩数据库日志文件
- 清理日志文件和压缩数据库
- SQL2005 压缩日志及数据库文件大小
- 数据库日志文件很大,但是无法压缩,无法清理,怎么办?
- 压缩日志及数据库文件大小[转]
- SQL Server压缩日志及数据库文件大小
- SQL Server 压缩日志及数据库文件大小
- 压缩日志及数据库文件大小
- SQL Server 压缩日志及数据库文件大小
- 压缩SQL Server 2005指定数据库文件和日志的大小
- mysqldump定时备份数据库,形成压缩文件,并记录dump日志
- 如何压缩SQL Server 2005指定数据库文件和日志的大小?
- sql server 2005 压缩实例下所有数据库文件及日志文件(转载)作者:Program Life