Script – Generate DBCC ShrinkFile for each datbase files


You can use below query to generate DBCC Shrikfile command for data and log file. Next step you have to copy the output from the result windows & execute it.

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'

 

Advertisements

4 thoughts on “Script – Generate DBCC ShrinkFile for each datbase files

  1. paul s

    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’

    Reply

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 )

Google+ photo

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

Connecting to %s