Script – Generate DBCC ShrinkFile for each datbase files

Posted by Jugal Shah on November 6, 2009


You can use below query to generate DBCC Shrikfile command for data and log file.

Query

select 'use ' + db_name(dbid) + char(13) + 'dbcc shrinkfile (' + quotename(sf.name,'''') + ' ,truncateonly)' from sysaltfiles sf
inner join sys.databases sd on sf.dbid = sd.database_id
where state_desc = 'online'

 

2 Responses to “Script – Generate DBCC ShrinkFile for each datbase files”

  1. paul s said

    There was a missing “sys” in your select for sys.sysaltfiles table. Here is the corrected syntax.
    –Generate DBCC SHRINKFILE commands
    select ‘use ‘ + db_name(dbid) + char(13) + ‘dbcc shrinkfile (‘ + quotename(sf.name,””) + ‘ ,truncateonly)’
    from sys.sysaltfiles sf
    inner join sys.databases sd on sf.dbid = sd.database_id
    where state_desc = ‘online’

Thanks for the comment, will get back to you soon... Jugal Shah

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 175 other followers