Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 programatically shrink all MDF files

Author  Topic 

rb1373
Yak Posting Veteran

93 Posts

Posted - 2005-01-03 : 14:27:35
I maintain a development server and the MDF files need to be shrunk occasionally to conserve space. What is the best way to automate this process? I'm thinking about using a while loop but I can't find a way to retrieve the MDF file names. Thanks.

- ray

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-01-03 : 14:35:23
use a scheduled job and
DBCC SHRINKDATABASE

Go with the flow & have fun! Else fight the flow
Go to Top of Page

rb1373
Yak Posting Veteran

93 Posts

Posted - 2005-01-03 : 14:38:44
I am using that command but it is not having the desired effect. The shrink file is more effective.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-01-03 : 14:55:31
ok... so what is your problem again?

Go with the flow & have fun! Else fight the flow
Go to Top of Page

rb1373
Yak Posting Veteran

93 Posts

Posted - 2005-01-03 : 15:20:53
I want to loop through all the databases and shrink the MDFs. I was stuck on capturing the file names, but I think I can capture this info using sp_helpfile.
Go to Top of Page

SQLServerDBA_Dan
Aged Yak Warrior

752 Posts

Posted - 2005-01-03 : 15:24:05
return LDF and MDF:
select filename from master..sysfiles
select filename from pubs..sysfiles
select filename from northwind..sysfiles

return MDF only:
select filename from master..sysfiles where filename like '%.mdf%'
select filename from pubs..sysfiles where filename like '%.mdf%'
select filename from northwind..sysfiles where filename like '%.mdf%'

enjoy.

Daniel
SQL Server DBA
Go to Top of Page

SQLServerDBA_Dan
Aged Yak Warrior

752 Posts

Posted - 2005-01-03 : 15:29:05
quote:
Originally posted by SQLServerDBA_Dan

return LDF and MDF:
select filename from master..sysfiles
select filename from pubs..sysfiles
select filename from northwind..sysfiles

return MDF only:
select filename from master..sysfiles where filename like '%.mdf%'
select filename from pubs..sysfiles where filename like '%.mdf%'
select filename from northwind..sysfiles where filename like '%.mdf%'

enjoy.

Daniel
SQL Server DBA



ok sorry. its looking for the logical name and not the physical.

you can get those like this:

SELECT FILE_NAME(1)
SELECT FILE_NAME(2)


Daniel
SQL Server DBA
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-01-05 : 23:56:23
I use this to get rid of unwanted LDF wasted space on DEV boxes (Sorry about the CURSOR chaps!)

Kristen

-- Script to build a script sutiable for truncated logs on all database
-- NOTE: Default state is to just "EXEC" the truncate command
-- Make sure "Results in Text" is set in QA - [Use Control-T]
-- Script-mode shows the size of LOGs for each DB;
-- you can truncate the lot, or use the Size as an indication of which ones
-- would benefit from truncated

SET NOCOUNT ON
DECLARE @strDatabase sysname,
@strSQL varchar(8000)

DECLARE CUR_Database CURSOR FOR
SELECT [name]
FROM master.dbo.sysdatabases
WHERE DATABASEPROPERTYEX(name, 'Status') <> 'OFFLINE'
AND DATABASEPROPERTYEX(name, 'Updateability') = 'READ_WRITE'
ORDER BY 1

OPEN CUR_Database
FETCH NEXT FROM CUR_Database into @strDatabase

WHILE @@FETCH_STATUS = 0
BEGIN
-- Build a SQL script to truncate this database.
-- NOTE: "GO" cannot appear at the beginning of a line, so {GO} is substituted later
-- NOTE: The blank lines separate each "job" a little
SELECT @strSQL =
'USE {DATABASE}
--{GO}
SELECT [--] = ''--'',
size,
[{DATABASE}] = name
FROM dbo.sysfiles
WHERE convert(varchar(255), [name]) LIKE ''%\_log'' ESCAPE ''\''
-- AND size > 256
ORDER BY 1
SELECT TOP 100
''USE {DATABASE}
{GO}
BACKUP LOG {DATABASE} WITH TRUNCATE_ONLY
{GO}
DBCC SHRINKFILE ('' + rtrim(name) +'', 1)
{GO}




''
FROM dbo.sysfiles
WHERE RTrim(name) LIKE ''%\_log'' ESCAPE ''\''
-- AND size > 256 -- Use this to only target "big ones"
ORDER BY 1'

SELECT @strSQL = REPLACE(REPLACE(@strSQL, '{DATABASE}', @strDatabase), '{GO}', 'GO')
-- SELECT [--SQL] = @strSQL -- For debugging
EXEC (@strSQL)

FETCH NEXT FROM CUR_Database into @strDatabase
END

CLOSE CUR_Database
DEALLOCATE CUR_Database
Go to Top of Page
   

- Advertisement -