經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

壓縮數(shù)據(jù)庫(kù)日志

系統(tǒng) 2051 0
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>

經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于是就寫(xiě)了一個(gè)通用的數(shù)據(jù)庫(kù)日志文件壓縮的存儲(chǔ)過(guò)程來(lái)解決此問(wèn)題:

/*--壓縮數(shù)據(jù)庫(kù)的通用存儲(chǔ)過(guò)程

壓縮日志及數(shù)據(jù)庫(kù)文件大小
因?yàn)橐獙?duì)數(shù)據(jù)庫(kù)進(jìn)行分離處理
所以存儲(chǔ)過(guò)程不能創(chuàng)建在被壓縮的數(shù)據(jù)庫(kù)中

--鄒建 2004.03(引用請(qǐng)保留此信息)--*/

/*--調(diào)用示例
exec p_compdb 'test'
--*/

use master --注意,此存儲(chǔ)過(guò)程要建在master數(shù)據(jù)庫(kù)中
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,--要壓縮的數(shù)據(jù)庫(kù)名
@bkdatabase bit=1,--因?yàn)榉蛛x日志的步驟中,可能會(huì)損壞數(shù)據(jù)庫(kù),所以你可以選擇是否自動(dòng)數(shù)據(jù)庫(kù)
@bkfname nvarchar(260)=''--備份的文件名,如果不指定,自動(dòng)備份到默認(rèn)備份目錄,備份文件名為:數(shù)據(jù)庫(kù)名+日期時(shí)間
as
--1.清空日志
exec('DUMP TRANSACTION ['+@dbname+'] WITH NO_LOG')

--2.截?cái)嗍聞?wù)日志:
exec('BACKUP LOG ['+@dbname+'] WITH NO_LOG')

--3.收縮數(shù)據(jù)庫(kù)文件(如果不壓縮,數(shù)據(jù)庫(kù)的文件不會(huì)減小
exec('DBCC SHRINKDATABASE(['+@dbname+'])')

--4.設(shè)置自動(dòng)收縮
exec('EXEC sp_dboption '''+@dbname+''',''autoshrink'',''TRUE''' )

--后面的步驟有一定危險(xiǎn),你可以可以選擇是否應(yīng)該這些步驟
--5.分離數(shù)據(jù)庫(kù)
if @bkdatabase=1
begin
if isnull(@bkfname,'')=''
set @bkfname=@dbname+'_'+convert(varchar,getdate(),112)
+replace(convert(varchar,getdate(),108),':','')
select 提示信息='備份數(shù)據(jù)庫(kù)到SQL 默認(rèn)備份目錄,備份文件名:'+@bkfname
exec('backup database ['+@dbname+'] to disk='''+@bkfname+'''' )
end

--進(jìn)行分離處理
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

--附加數(shù)據(jù)庫(kù)
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




經(jīng)常在CSDN上看到網(wǎng)友發(fā)帖說(shuō),壓縮日志文件處理不當(dāng),導(dǎo)致數(shù)據(jù)庫(kù)損壞,甚至不能恢復(fù)數(shù)據(jù),于

分享到:
評(píng)論

壓縮數(shù)據(jù)庫(kù)日志


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 喀喇沁旗| 宿迁市| 营口市| 原阳县| 临江市| 德江县| 宁夏| 黄冈市| 武定县| 新津县| 襄城县| 南宫市| 桂东县| 珲春市| 白河县| 嘉祥县| SHOW| 阳春市| 土默特右旗| 江永县| 龙川县| 上思县| 沾化县| 南岸区| 察雅县| 苗栗县| 静安区| 张家港市| 安吉县| 东宁县| 娄烦县| 藁城市| 门源| 泌阳县| 民权县| 海安县| 元氏县| 扶绥县| 汽车| 南川市| 安西县|