欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL语句实现SQL Server 2000及Sql Server 2005日志收缩(批量)

程序员文章站 2022-06-29 14:30:02
复制代码 代码如下:declare @name varchar(25) declare @sql varchar(1000) declare @logid int decl...

复制代码 代码如下:

declare @name varchar(25)
declare @sql varchar(1000)
declare @logid int

declare sysdatabase_name cursor for select name from master.dbo.sysdatabases

open sysdatabase_name


fetch next from sysdatabase_name into @name

while @@fetch_status = 0
begin

if ( @name not in ('xxx')) --不需要进行日志收缩的数据库名
begin
set @sql =' declare @logid int
use ' + @name+'
select @logid = fileid from sysfiles where right(rtrim(filename),3) = ''ldf''

backup log ' + @name+' with no_log
dbcc shrinkfile (@logid) '

exec(@sql)

end

fetch next from sysdatabase_name into @name

end

close sysdatabase_name
deallocate sysdatabase_name

sql语句实现sql server 2005日志收缩(批量)
复制代码 代码如下:

-->title:生成測試數據
-->author:wufeng4552
-->date :2009-09-15 08:56:03
declare @dbname nvarchar(20) --數據庫名稱
declare @sql nvarchar(max)
declare sysdbname cursor for select name from master.dbo.sysdatabases
open sysdbname
fetch next from sysdbname into @dbname
while @@fetch_status=0
begin
if (@dbname not in('xxx'))--不需要进行日志收缩的数据库名
begin
set @sql=--日誌文件id
n' declare @logid int '+
n' use '+@dbname+
n' select @logid=fileid from sysfiles where right(ltrim(rtrim(filename)),3)=''ldf'''+
--截断事务日志
n' backup log '+@dbname+' with no_log '+
--收缩指定数据文件
n' dbcc shrinkfile(@logid)'
exec(@sql)
end
fetch next from sysdbname into @dbname
end
close sysdbname
deallocate sysdbname