views:

41

answers:

1

Hi I am using this script in the process of weekly maintenance, suggest best approach/scripts to do shrinklog. Currently am getting an error with the below script

declare @s nvarchar(4000)
set @s= '
        if ''?'' not in (''tempdb'',''master'',''model'',''msdb'') 
        begin
            use [?] 
            Alter database [?] SET Recovery simple
        end '

exec sp_msforeachdb @s
set @s= '
        if ''?'' not in (''tempdb'',''master'',''model'',''msdb'') 
        begin     
            use [?]  
            Declare @LogFileLogicalName sysname
            select @LogFileLogicalName=Name from sys.database_files where Type=1
            DBCC Shrinkfile(@LogFileLogicalName,1) 
        end'
exec sp_msforeachdb @s

Error Description:

ShrinkLog Execute SQL Task Description: Executing the query "declare @s nvarchar(4000) set @s= ' ..." failed with the following error: "Option 'RECOVERY' cannot be set in database 'tempdb'. Cannot shrink log file 2 (DBServices_Log) because total number of logical log files cannot be fewer than 2. DBCC execution completed. If DBCC printed error messages, contact your system administrator.

note: I am avoiding tempdb(all System db) in my script, but error message shows tempdb?

+2  A: 

This is probably the worst maintenance script I've seen in the past year. A maintenance script that every week breaks the log chain and makes the database unrecoverable?? OMG. Not to mention that the simple premise of shrinking the log on a maintenance task is wrong. If the database log has grown to a certain size, than that size is needed. Schedule log backups more frequently to prevent this, but don't schedule log shrink operations.

Remus Rusanu
Am appriciate your suggestion, want to know what is the wrong with this script.
rmdussa
Two things are wrong: 1) `Alter database [?] SET Recovery simple` will break the log chain of any database *not* in simple recovery. and 2) The premise of the script is wrong, that you need to shrink the logs periodically. Logs are kept small by taking frequent log backups (if not already in simple recovery mode). Shrinking is an extraordinary operation done only on special occasions, **never** on a regular scheduled maintenance.
Remus Rusanu
Please look at the note and respond,
rmdussa
about the note: certain validations are done at compile time, not at execution time. So even though your batch contains `ALTER DATABASE [tempdb] SET RECOVER ...` in an IF branch that is never *executed*, it is still compiled and the error 5058 is raised.
Remus Rusanu
Thank you very much for your suggestions.
rmdussa
I do not know Why I got -ve mark, Please let me know,anything wrong
rmdussa