下面有段代码,可行:/* 关闭用户打开的进程处理 */ use master goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[p_killspid] GOcreate proc p_killspid @dbname varchar(200) --要关闭进程的数据库名 as declare @sql nvarchar(500) declare @spid nvarchar(20) declare #tb cursor for select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname) open #tb fetch next from #tb into @spid while @@fetch_status=0 begin exec('kill '+@spid) fetch next from #tb into @spid end close #tb deallocate #tb go/*--压缩数据库的通用存储过程
压缩日志及数据库文件大小 *--yyj 2004.3--*/use master goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_compdb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[p_compdb] GOcreate proc p_compdb @dbname sysname, --要压缩的数据库名 @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+'])')--4.设置自动收缩 exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''')--后面的步骤有一定危险,你可以可以选择是否应该这些步骤 --5.分离数据库 if @bkdatabase=1 begin if isnull(@bkfname,'')='' set @bkfname=@dbname+'_'+convert(varchar,getdate(),112) +replace(convert(varchar,getdate(),108),':','') select 提示信息='备份数据库到SQL 默认备份目录,备份文件名:'+@bkfname exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''') 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)+'"' exec master..xp_cmdshell @s,no_output 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) go/*--调用示例*/ use master --关闭用户进程 exec p_killspid 'ChinaSchool_RS40'--压缩数据库 exec p_compdb 'ChinaSchool_RS40'--改变日志文件的默认属性,其增长最大值为500M ALTER DATABASE ChinaSchool_RS40 MODIFY FILE (NAME = 'ChinaSchool_RS40_log', MAXSIZE = 500MB) GO/*--*/
TO hnhn() 请问你测试过这个存储过程嘛.能否对一个数据库,有多个日志的进行成功压缩呢. 40G的日志,大概能压缩多少呢. 上面的存储过程是否重新产生日志文件呢.TO Haiwer(海阔天空) 多谢你的解决方案,太感谢了.测试中...
backup log 数据库名 with no_log --提供有关所有数据库中的事务日志空间使用情况的统计信息。
dbcc sqlperf (logspace) --收缩数据库
dbcc shrinkdatabase (数据库名, truncateonly) 说明:
TRUNCATEONLY 将数据文件中的任何未使用的空间释放给操作系统,并将文件收缩到上一次所分配的大小,从而减少文件大小,而不移动任何数据。特定数据库的所有数据和日志文件。执行 DBCC SHRINKDATABASE
一次一个特定数据库中的数据或日志文件。执行 DBCC SHRINKFILE
关闭用户打开的进程处理
*/
use master
goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_killspid]
GOcreate proc p_killspid
@dbname varchar(200) --要关闭进程的数据库名
as
declare @sql nvarchar(500)
declare @spid nvarchar(20) declare #tb cursor for
select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
open #tb
fetch next from #tb into @spid
while @@fetch_status=0
begin
exec('kill '+@spid)
fetch next from #tb into @spid
end
close #tb
deallocate #tb
go/*--压缩数据库的通用存储过程
压缩日志及数据库文件大小
*--yyj 2004.3--*/use master
goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_compdb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_compdb]
GOcreate proc p_compdb
@dbname sysname, --要压缩的数据库名
@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+'])')--4.设置自动收缩
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''')--后面的步骤有一定危险,你可以可以选择是否应该这些步骤
--5.分离数据库
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='备份数据库到SQL 默认备份目录,备份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''')
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)+'"'
exec master..xp_cmdshell @s,no_output
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)
go/*--调用示例*/
use master
--关闭用户进程
exec p_killspid 'ChinaSchool_RS40'--压缩数据库
exec p_compdb 'ChinaSchool_RS40'--改变日志文件的默认属性,其增长最大值为500M
ALTER DATABASE ChinaSchool_RS40
MODIFY FILE
(NAME = 'ChinaSchool_RS40_log',
MAXSIZE = 500MB)
GO/*--*/
请问你测试过这个存储过程嘛.能否对一个数据库,有多个日志的进行成功压缩呢.
40G的日志,大概能压缩多少呢.
上面的存储过程是否重新产生日志文件呢.TO Haiwer(海阔天空)
多谢你的解决方案,太感谢了.测试中...