Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I am using SQL Server 2008 R2 and creating databases through continuous deployment.

In our system, the default 1Mb/10% autogrowth settings in SQL Server work badly with our data. Particularly as we have a legacy application that prevents us from changing the schema much. We would like the database settings to be configured at the instance level so we can change it for staged deployments.

I have read in several places that default settings for a new db are based on the settings for 'model', but it appears this only works by clicking new database in SQL Management studio UI and not from a script e.g. CREATE DATABASE [MyDb].

msdn.microsoft.com/en-us/library/ms186388(v=sql.105).aspx

sqlservercentral.com/Forums/Topic1065073-391-1.aspx

Possible to configure database autogrowth settings at the instance level?

Has anyone actually got this to work with a create script? Is there another way I can set autogrowth per server instance?

share|improve this question

migrated from stackoverflow.com Jul 31 '12 at 17:11

This question came from our site for professional and enthusiast programmers.

5  
If you're scripting it out with CREATE DATABASE why can't you just specify it in your script? –  JNK Jul 31 '12 at 17:17
1  
@JNK I think he wants it to inherit rather than having to check what it should be. And it should do that, but I have to agree that it doesn't (and it is not doing that in 2012 either). I actually didn't believe it until I tried it - I am sure this worked correctly at some point. Maybe a regression from bug fixes for that 10000% thing. –  Aaron Bertrand Jul 31 '12 at 18:48
    
In that case are those figures available in a DMV somewhere? could you possibly script it out by querying system tables or DMVs for appropriate values?' –  JNK Jul 31 '12 at 18:54
    
To clarify, We would rather inherit. We have a large number of db servers with varying data storage across our staged environments and controlling script changes per environment would be more work at this stage. @JNK's idea is one I hadn't thought of. It looks like there may be a query that will help: beyondrelational.com/modules/2/blogs/28/posts/10326/… –  Michelle Steele Aug 1 '12 at 9:37

2 Answers 2

For SQL Server 2005+ You can use the following:

SELECT FILE_ID, type, type_desc, 
CAST( CAST( (CASE WHEN size < 128 THEN 128 ELSE size END) * 8. / 1024. AS INT ) AS VARCHAR(20) ) + 'MB' AS SIZE, 
CASE max_size WHEN -1 THEN 'UNLIMITED' ELSE CAST( CAST( max_size * 8. / 1024. AS INT ) AS VARCHAR(20) ) + 'MB' END  AS MAXSIZE,
CASE is_percent_growth
    WHEN 0 THEN CAST( CAST( growth * 8. / 1024. AS INT ) AS VARCHAR(20) ) + 'MB'
    WHEN 1 THEN CAST( growth AS VARCHAR(30) ) + '%'
END AS FILEGROWTH
FROM sys.master_files
WHERE database_id = DB_ID('model')
GO

This will retrieve File definitions for Model Database.
Later on You can use it either in CREATE DATABASE or ALTER DATABASE.

SSMS actually uses SMO to retrieve these parameters and then creates a Script which looks like:

CREATE DATABASE [aaa] ON  PRIMARY 
( NAME = N'aaa', FILENAME = N'D:\Data\aaa.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED , FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'aaa_log', FILENAME = N'D:\Logs\aaa_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)
GO

If Your Application is creating the Database and You can't modify the application -
then You have to use ALTER DATABASE after creation is done:

ALTER DATABASE [aaa] MODIFY FILE ( NAME = N'aaa', MAXSIZE = 102400KB )
GO
ALTER DATABASE [aaa] MODIFY FILE ( NAME = N'aaa_log', FILEGROWTH = 10240KB )
GO

This requires at least ALTER DATABASE Permission
(implied by ALTER ANY DATABASE Server Permission)
Obviously, You can generate that script using the first code sample (FROM sys.master_files).

I wouldn't use sysaltfiles for SQL Server 2008R2 because it is Deprecated.

Good luck,
Roi

share|improve this answer

How about:

use master
go

declare @newDbName sysname
, @fn nvarchar(1024)
, @growthAmount integer
, @growth nvarchar(128)
, @growthAmount_log integer
, @growth_log nvarchar(128)

set @newDbName = 'myNewDB'

--see what's available to us
--select * 
--from sys.sysaltfiles saf
--inner join sys.databases db
--  on saf.dbid = db.database_id
--where db.name = 'model'

select @growthAmount = saf.growth 
, @growth = case saf.status & 0x100000 when 0 then 'MB' else '%' end
, @fn = replace(saf.filename,'model.mdf','')
from sys.sysaltfiles saf
inner join sys.databases db
    on saf.dbid = db.database_id
where db.name = 'model'
and saf.name = 'modeldev'

select @growthAmount_log = saf.growth 
, @growth_log = case saf.status & 0x100000 when 0 then 'MB' else '%' end
from sys.sysaltfiles saf
inner join sys.databases db
    on saf.dbid = db.database_id
where db.name = 'model'
and saf.name = 'modellog'

if @growth = 'MB' set @growthAmount = @growthAmount / 128
if @growth_log = 'MB' set @growthAmount_log = @growthAmount_log / 128

set @growth = cast(@growthAmount as nvarchar(10)) + @growth
set @growth_log = cast(@growthAmount_log as nvarchar(10)) + @growth_log

print '
    CREATE DATABASE ' + @newDbName + '
    ON 
    (   
        NAME = ' + @newDbName + '_data
        ,FILENAME = ''' + @fn + @newDbName + '.mdf''
        ,FILEGROWTH = ' + @growth + '
    )
    LOG ON
    ( 
        NAME = ' + @newDbName + '_log
        ,FILENAME = ''' + @fn + @newDbName + '.ldf''
        ,FILEGROWTH = ' + @growth_log + '
    ) 
'
exec('
    CREATE DATABASE ' + @newDbName + '
    ON 
    (   
        NAME = ' + @newDbName + '_data
        ,FILENAME = ''' + @fn + @newDbName + '.mdf''
        ,FILEGROWTH = ' + @growth + '
    )
    LOG ON
    ( 
        NAME = ' + @newDbName + '_log
        ,FILENAME = ''' + @fn + @newDbName + '.ldf''
        ,FILEGROWTH = ' + @growth_log + '
    ) 
')
share|improve this answer
    
ps. more info on this table here: msdn.microsoft.com/en-us/library/ms181338.aspx. In my code above I haven't accounted for the maxsize=0 case (i.e. no growth), and have masked the status code since I had a different figure for percentages to what's in the documentation, but was able to make the values comparable via masking. I'm not worried about the 0 status code, since that's taken care of by maxsize (were it implemented). –  JohnLBevan Aug 13 '12 at 14:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.