views:

996

answers:

4

I am trying to grow a database using the following the code below. However I get the following error. How do I check for size (3600MB) and grow it if necessary?

USING MyDatabase

ALTER DATABASE MyDatabase
MODIFY FILE (NAME =  MyDatabase_data, SIZE = 3600MB)

Error: MODIFY FILE failed. Specified size is less than or equal to current size.

UPDATE: I am not using Auto Grow due to the heavy traffic. Due to the setup I have here (long unrelated story) I need to make the change using code. However, if this code runs more than once I get the error described above. I need to check the size first before attempting the change again.

+2  A: 

The database will automatically grow. If you (MS SQL Server anyway) right click on the database, properties. You can set it grow by a percentage, say 10%, or by a fixed size, say 100Mb.

You should not need to maintain the database size yourself.

Simon Hughes
Constant mdf grows will lead to fragmentation and tax the server - iirc the entire database is locked during a growth. Setting a large size when you know it will grow is a good practice.
Sam
+1 Sam. Database not locked, but under SQL 2000 dramatically stressed! less so SQL 2005. AutoGrow will happen when the system is busy (i.e. when user activity creates data) and growing a large database by 10% at a busy period will slow requests and may cause timeouts
Kristen
We use SysInternal's CONTIG utility to remove fragmentation from database files. It can defrag a single file. Never been brave enough to do it with database UP! http://technet.microsoft.com/en-us/sysinternals/bb897428.aspx
Kristen
+1  A: 

MS SQL allocates more space than it needs. So your actual used database size is often much smaller than the allocated size.

MS SQL allocates more space than it needs because the allocation of more data is pretty expensive and it's more efficient to do it periodically instead of on each write to the DB.

If you want to alter how much space is allocated at a time, you can use the FILEGROWTH parameter to set up the type of file growth you'd like on your database.

A value of:

  • 0 means there is no growth allowed
  • [X]MB or [X]GB means to grow by that much when your DB current size is reached
  • [X]% means to grow by that much percent when your DB current size is reached

.

ALTER DATABASE [databasename] MODIFY FILE ( NAME = N'databasename', FILEGROWTH = 1GB )

To free up the extra allocated space you can perform a DBCC SHRINKDATABASE

To obtain the actual size of your database you can execute this:

SELECT size FROM sys.database_files WHERE name = 'databasename'

To obtain the actual size of your database log you can execute this:

SELECT size FROM sys.database_files WHERE name = 'databasename_log'
Brian R. Bondy
+1  A: 

To get the size (and other information) of all database files on the server (obviously you can filter this):

select * from sys.database_files
Richard
A: 

The size of the DB will be shown by

USE MyDatabase
EXEC sp_spaceused

looking at the code for sp_spaceused (I happen to be looking at a SQL 2000 server, but same/similar would be true for SQL2005 / SQL2008)

USE master
EXEC sp_helptext 'sp_spaceused'

and the relevant code is:

declare @dbsize dec(15,0)
declare @logsize dec(15)
declare @bytesperpage   dec(15,0)
declare @pagesperMB  dec(15,0)

    select @dbsize = sum(convert(dec(15),size))
     from dbo.sysfiles
     where (status & 64 = 0)

    select @logsize = sum(convert(dec(15),size))
     from dbo.sysfiles
     where (status & 64 <> 0)

    select @bytesperpage = low
     from master.dbo.spt_values
     where number = 1
      and type = 'E'
    select @pagesperMB = 1048576 / @bytesperpage

    select  database_name = db_name(),
     database_size =
      ltrim(str((@dbsize + @logsize) / @pagesperMB,15,2) + ' MB')

so from that you could save the size to a variable and compare again the size you were trying to set:

DECLARE @database_size bigint
SELECT  @database_size = (@dbsize + @logsize) / @pagesperMB
IF @database_size < 3600
BEGIN
    PRINT 'Expanding MyDatabase ...'
    ALTER DATABASE MyDatabase
    MODIFY FILE (NAME =  MyDatabase_data, SIZE = 3600MB)
    PRINT 'Expanding MyDatabase DONE'
END
ELSE
BEGIN
    PRINT 'No expansion of MyDatabase required'
END
Kristen