views:

291

answers:

3

I want to create an SQL script that creates a database. Right now, I have this:

CREATE DATABASE [Documents] ON  PRIMARY 
( NAME = N'Documents', FILENAME = N'Documents.mdf')
 LOG ON 
( NAME = N'Documents_log', FILENAME = N'Documents_log.ldf')
 COLLATE SQL_Latin1_General_CP1_CI_AS

However, this generates the following error:

Msg 5105, Level 16, State 2, Line 2
A file activation error occurred. The physical file name 'Documents.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation.
Msg 1802, Level 16, State 1, Line 2
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

I know the problem is that I did not specify fully qualified path for the filenames. But I want to be able to run this script regardless of the directory structure of the database server. Is there some way to use a default path?

+1  A: 

Take a Look on how to create a Default Path or at Create Database. See if it helps on what you are looking for.

Cheers,

Goows
+2  A: 

You can create a database without specifying file details, like:

CREATE DATABASE Documents;

Damir Sudarevic
True, but what if I need to specify additional attributes like SIZE and FILEGROWTH? I've seen the solutions using XP's to read the registry but what if you aren't running on the default instance? That registry key/value wouldn't give the right answer.Still searching so I don't really consider this answered myself.
Christopher Painter
A: 

I believe that you can do

CREATE DATABASE [Documents]

without the ON .... and it will get created with defaults for path and the rest.

Nestor