I write a simple procedure to shrink database log,just need to input the database name:
ALTER proc [dbo].[sp_shrinkDBAndDBLog]
@databaseName nvarchar(100)
as
begin
declare @logName nvarchar(100),
@dynamicSQL nvarchar(500)
set @dynamicSQL='ALTER DATABASE '+@databaseName+' SET RECOVERY SIMPLE WITH NO_WAIT'
exec(@dynamicSQL)
--select name from HLJEDI_SYS.sys.sysfiles where groupid=0;
set @dynamicSQL=N'select @logName= name from '+@databaseName+'.sys.database_files where type_desc=''LOG'''
exec sp_executesql @dynamicSQL,N'@logName nvarchar(100) output',@logName output
--select * from sys.sysfiles where groupid=0
set @dynamicSQL='DBCC SHRINKFILE (N'''+@logName+''',11,TRUNCATEONLY)'
exec(@dynamicSQL)
--DBCC SHRINKFILE (N'CUC_OA_LOG' , 11, TRUNCATEONLY)
set @dynamicSQL='ALTER DATABASE '+@databaseName+' SET RECOVERY FULL WITH NO_WAIT'
exec(@dynamicSQL)
--ALTER DATABASE OA SET RECOVERY FULL --(Restore to Full Schema)
end
but when i execute with:
exec sp_shrinkDBAndDBLog 'DBName'
it has error: sys.database_files can't find database 'master' file 'eca2_log'。The file was been deleted and not exists。
And what is the problem? Thank you if you tell me and show the detail and princeple.
看起来您在执行DBCC SHRINKFILE命令之前可能缺少USE {database}命令,因此未在正确的数据库上执行DBCC。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.