經(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ù),于
- 2008-04-12 20:06
- 瀏覽 175
- 評(píng)論(0)
- 相關(guān)推薦
發(fā)表評(píng)論
更多文章、技術(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ì)您有幫助就好】元

評(píng)論