对网上流传的SQL日志清理脚本的小小改进
2008-04-18 20:06
302 查看
前段时间,发现下面一个单位的MSSQL日志有35G,而数据文件只4G多,貌似有点问题。
所以从网上找来了一个清理脚本。
因为转载的太多,也不知是哪位原创,这里就不引用原文了。原作者见谅哦。
发现的小问题,脚本中的危险操作没有加判断便会执行,所以改了下。
放在这里也是为哪天移动硬盘崩了,还能找回来。
use master--注意:该存储过程要建在master数据库中
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_compdb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_compdb]
GO
create proc p_compdb
@dbname sysname,--要压缩的数据库名
@isdel bit=0,
@bkdatabase bit=1,--在分离日志的步骤中,可能会损坏数据库,可以选择是否自动备份数据库
@bkfname nvarchar(260)='' --备份的文件名,如果不指定,自动备份到默认备份目录,备份文件名为:数据库名+日期时间
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')
--2.截断事务日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')
--3.收缩数据库文件(如果不压缩,数据库的文件不会减小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')
--也可以用以下的方式:
--收缩指定数据文件,1是文件号,可以通过这个语句查询到:select * from sysfiles
--DBCC SHRINKFILE(1)
--4.设置自动收缩
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''')
--也可以通过企业管理器进行操作
--企业管理器->右键你要压缩的数据库->所有任务->收缩数据库->收缩文件
--选择日志文件->在收缩方式里选择收缩至XXM,这里会给出一个允许收缩到的最小M数,直接输入这个数,确定就可以了
if @isdel=1
begin
--后面的步骤有一定危险,请确保做好了数据库备份再选择下列的步骤
--5.分离数据库
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
begin
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)+replace(convert(varchar,getdate(),108),':','')+'.ext'
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''')
select 备份成功='备份数据库到SQL 默认备份目录,备份文件名:'+@bkfname
end
else
begin
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''')
select 备份成功='备份数据库为:'+@bkfname
end
end
--进行分离处理
create table #t(fname nvarchar(260),type int)
exec('insert into #t select filename,type=status&0x40 from ['+@dbname+']..sysfiles')
exec('sp_detach_db '''+@dbname+'''')
--删除日志文件
declare @fname nvarchar(260),@s varchar(8000)
declare tb cursor local for select fname from #t where type=64
open tb
fetch next from tb into @fname
while @@fetch_status=0
begin
set @s='del "'+rtrim(@fname)+'"'
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[xp_cmdshell]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
begin
exec master..xp_cmdshell @s,no_output
end
else
begin
select 错误='无法使用xp_cmdshell!如是SQL2K5需先启用xp_cmdshell(参考sp_configure)'
select 请手工删除日志文件#然后再附加数据库=rtrim(@fname)
goto quit
end
fetch next from tb into @fname
end
close tb
deallocate tb
--附加数据库
set @s=''
declare tb cursor local for select fname from #t where type=0
open tb
fetch next from tb into @fname
while @@fetch_status=0
begin
set @s=@s+','''+rtrim(@fname)+''''
fetch next from tb into @fname
end
close tb
deallocate tb
exec('sp_attach_single_file_db '''+@dbname+''''+@s)
quit:
end
go
--exec p_compdb 'test11',true,true,'c:\bak.ext'
所以从网上找来了一个清理脚本。
因为转载的太多,也不知是哪位原创,这里就不引用原文了。原作者见谅哦。
发现的小问题,脚本中的危险操作没有加判断便会执行,所以改了下。
放在这里也是为哪天移动硬盘崩了,还能找回来。
use master--注意:该存储过程要建在master数据库中
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_compdb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_compdb]
GO
create proc p_compdb
@dbname sysname,--要压缩的数据库名
@isdel bit=0,
@bkdatabase bit=1,--在分离日志的步骤中,可能会损坏数据库,可以选择是否自动备份数据库
@bkfname nvarchar(260)='' --备份的文件名,如果不指定,自动备份到默认备份目录,备份文件名为:数据库名+日期时间
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')
--2.截断事务日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')
--3.收缩数据库文件(如果不压缩,数据库的文件不会减小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')
--也可以用以下的方式:
--收缩指定数据文件,1是文件号,可以通过这个语句查询到:select * from sysfiles
--DBCC SHRINKFILE(1)
--4.设置自动收缩
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''')
--也可以通过企业管理器进行操作
--企业管理器->右键你要压缩的数据库->所有任务->收缩数据库->收缩文件
--选择日志文件->在收缩方式里选择收缩至XXM,这里会给出一个允许收缩到的最小M数,直接输入这个数,确定就可以了
if @isdel=1
begin
--后面的步骤有一定危险,请确保做好了数据库备份再选择下列的步骤
--5.分离数据库
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
begin
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)+replace(convert(varchar,getdate(),108),':','')+'.ext'
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''')
select 备份成功='备份数据库到SQL 默认备份目录,备份文件名:'+@bkfname
end
else
begin
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''')
select 备份成功='备份数据库为:'+@bkfname
end
end
--进行分离处理
create table #t(fname nvarchar(260),type int)
exec('insert into #t select filename,type=status&0x40 from ['+@dbname+']..sysfiles')
exec('sp_detach_db '''+@dbname+'''')
--删除日志文件
declare @fname nvarchar(260),@s varchar(8000)
declare tb cursor local for select fname from #t where type=64
open tb
fetch next from tb into @fname
while @@fetch_status=0
begin
set @s='del "'+rtrim(@fname)+'"'
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[xp_cmdshell]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
begin
exec master..xp_cmdshell @s,no_output
end
else
begin
select 错误='无法使用xp_cmdshell!如是SQL2K5需先启用xp_cmdshell(参考sp_configure)'
select 请手工删除日志文件#然后再附加数据库=rtrim(@fname)
goto quit
end
fetch next from tb into @fname
end
close tb
deallocate tb
--附加数据库
set @s=''
declare tb cursor local for select fname from #t where type=0
open tb
fetch next from tb into @fname
while @@fetch_status=0
begin
set @s=@s+','''+rtrim(@fname)+''''
fetch next from tb into @fname
end
close tb
deallocate tb
exec('sp_attach_single_file_db '''+@dbname+''''+@s)
quit:
end
go
--exec p_compdb 'test11',true,true,'c:\bak.ext'
相关文章推荐
- 清理日志脚本sql
- MS SQL SERVER 清理数据库日志脚本
- 如何清理SQL 2008日志脚本
- Scripts:基于时间显示闪回日志中redo的脚本fdb_redo_time_matrix.sql
- python小脚本 自动清理大于七天的Tomcat日志
- ms sqlserver 清除数据库日志脚本
- SQL日志文件太大,清理方法
- shell清理日志脚本
- 清理SQL日志
- DB2定时清理归档日志脚本
- 如何清理sql server数据库日志
- linux通用自动清理日志脚本
- Linux定时清理30天前的Tomcat日志脚本
- SQL脚本--有关压缩数据库日志
- SQL清除所有数据库日志脚本
- Tomcat日志文件定时清理备份的脚本
- SQL脚本积累之二-----SQL Server数据库日志删除脚本
- 转(zjcxc)之SQL脚本--有关压缩数据库日志
- 利用SQL语句清理日志
- (运维日志)对Excel进行数据导入,函数拼写Sql脚本,动态生成一列单元格函数