4

We have a few test databases that we are throwing test indexes into and the log file gets bloated real quickly due to us dropping the contents of the table and repopulating it.

I have found, thanks to Stack Overflow, a few scripts and put them together to do what I need.

Here is the script:

USE SSSIndexes
GO
ALTER DATABASE SSSIndexes SET RECOVERY SIMPLE WITH NO_WAIT
GO
DBCC SHRINKFILE(N'SSSIndexes_Log', 1)   <-- my issue is here
GO

The problem is the log file name. Is there a way to get the log file name without having to look it up manually and include it in the script to where this part is automated?

Btw, we never intend on restore this database. These are temporary indexes.

Thanks!

0

3 Answers 3

10
USE SSSIndexes
GO
ALTER DATABASE SSSIndexes SET RECOVERY SIMPLE WITH NO_WAIT
GO
DECLARE @Name NVARCHAR(50)

DECLARE cur CURSOR FOR
SELECT [name]
FROM [sys].[database_files] 
where [type] = 1

OPEN cur
FETCH NEXT FROM cur INTO @Name
WHILE @@FETCH_STATUS = 0
BEGIN
    DBCC SHRINKFILE(@Name, 1)
    FETCH NEXT FROM cur INTO @Name
END
CLOSE cur
DEALLOCATE cur
Sign up to request clarification or add additional context in comments.

2 Comments

If I am reading this correctly, it is shinking all databases in that instance? Am I right in thinking this?
No, only one database, see to the 1st row: USE SSSIndexes. [sys].[database_files] is specific for each data base.
0

You can use this to generate script for log file truncating of all databases on a specific server. To stick to specific databases use a filter.

SELECT
' USE [' + name + ']; GO

-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE [' + name + '] SET RECOVERY SIMPLE;
GO

-- Shrink the truncated log file to 1 MB.
DECLARE @logname varchar(128);
SELECT TOP 1 @logname=[name] FROM [' + name + '].[sys].[database_files] WHERE [type] = 1;
DBCC SHRINKFILE (@logname, 1);
GO

-- Reset the database recovery model.
ALTER DATABASE [' + name + '] SET RECOVERY FULL;
GO

' AS qry FROM master.dbo.sysdatabases
WHERE dbid > 6

Comments

0

WARNING!!!: YOU SHOULD ONLY DO THIS ON TEST DB SERVERS. Production DBs typically WANT to have FULL recovery mode. Test DBs you usually don't care.

Expanding on Abdul's answer (I had trouble getting it to show in new lines) and Dis' answers this finds the first db that has recovery FULL, sets it to simple and shrinks it... just keep clicking execute until it returns null

declare @dbname nvarchar(50)
declare @logfilename nvarchar(100)
select top(1) @dbname = name from sys.databases where recovery_model_desc <> 'SIMPLE' AND name <> 'tempdb'

declare @shrinkdb nvarchar(500)
set @shrinkdb = 'USE master 
ALTER DATABASE [' + @dbname + '] SET RECOVERY SIMPLE 
'
select @shrinkdb
execute sp_executesql @shrinkdb

set @shrinkdb = 'USE [' + @dbname + ']

DECLARE @Name NVARCHAR(50)

DECLARE cur CURSOR FOR
SELECT [name]
FROM [sys].[database_files] 
where [type] = 1

OPEN cur
FETCH NEXT FROM cur INTO @Name
WHILE @@FETCH_STATUS = 0
BEGIN
    DBCC SHRINKFILE(@Name, 1)
    FETCH NEXT FROM cur INTO @Name
END
CLOSE cur
DEALLOCATE cur

'

select @shrinkdb
execute sp_executesql @shrinkdb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.