Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Grow\Shrink all datafiles SQL 2005\2008

By Allan Roodt,

This script is essentially a generator which outputs a script in the query window which will grow all the datafiles ( or shrink them ) to the same amount as specified. This saves time having to write out long queries for multiple databases with multiple datafiles. The main purpose behind this script is if the user works on a system where the datafiles has to be the same size to ensure consistency, this script will help them out.

If the user enters an amount ( maybe by accident or on purpose ) which is less than what the current size of the primary MDF datafile ( which to my knowlege is usually the biggest ), an added security feature is in place warning the user that a shrink will occur, though if this is what you want, you have to provide a special parameter ( @vcYES which is outputted in the query ) to acknowlege that you are sure you want to continue ( think of it as SQL's way of clicking YES or no like in Windows ).

1)To use this script, paste it in a query window and connect to the database you want to work on.
2)Enter the current database name you are on in the @vcDatabaseName variable
3)Indicate to how many MB you want to grow\shrink the datafile in the @vcGrowAmountMB variable
4)Run the script and copy the result outputted into a new query window. This will be the script which does the work for you.
5)Do something else because now you have alot of free time :)

Total article views: 2251 | Views in the last 30 days: 1
Related Articles

Delete Datafiles



SQL Datafile... How to protect ????

SQL Datafile... How to protect ????


Windows Group Membership Checker

A script to reveal which logins have access to your sql server via windows groups.


SQL Profiler v/s Query window

SQL Profiler v/s Query window


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones