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)
 Shrink Problems

Author  Topic 

sweko
Starting Member

5 Posts

Posted - 2005-02-20 : 08:21:26
I am trying to shrink a database that has (almost) filled the disk the files are on.
As soon as I issue the SHRINKDATABASE, the transaction log start groing out of proportions, fills the disk completely, and breaks the shrink operation.

The database is running in single user mode, and the recovery model is simple.

I just can't understand what is going on, and i really need that disk space.

--
SWeko has spoken

Kristen
Test

22859 Posts

Posted - 2005-02-20 : 10:01:13
There are almost certianly better scripts on the Net, but (with a bit of luck) this one will get the correct names for the Logical Log files etc. and create something that you can then run to shrink all (or selectively, some) databases

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 -- Enable this line if you only want logs "bigger" than a specific size
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

Kristen
Go to Top of Page
   

- Advertisement -