Frage

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.

War es hilfreich?

Lösung

Looks like you might be missing a USE { database } command before you execute the DBCC SHRINKFILE command, and so the DBCC is not getting executed on the correct database.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top