Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create database using script at the default path?

I've generated a script of a database in SQL Server 2008. The generated script has the hardcoded path of where the database would be created. I don't want this path to be hardcoded, I want this path to use the default of the database engine the script is running on.

Here is the small portion of the script:

CREATE DATABASE [POS] ON  PRIMARY  ( NAME = N'POS', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\POS.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  LOG ON  ( NAME = N'POS_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\POS_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) GO 

The path C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\POS.mdf might not exist on all computers that's why I want it to be chosen by the database engine

like image 750
Uzair Farooq Avatar asked Jan 12 '12 15:01

Uzair Farooq


People also ask

How do I create a database script?

Generate a Script in the SQL Server Management Studio Open SQL Server Management Studio (SSMS) Expand Databases. Select the database to script. Right-click on the database and select Tasks > Generate Scripts.


2 Answers

Simply create the database and then adjust all the needed properties directly in files

CREATE DATABASE [POS]  GO ALTER DATABASE POS MODIFY FILE  ( NAME = N'POS' , SIZE = 3048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) GO ALTER DATABASE POS MODIFY FILE  ( NAME = N'POS_log' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) GO 
like image 105
Oleg Dok Avatar answered Oct 18 '22 21:10

Oleg Dok


Why don't use just:

CREATE DATABASE [POS]; 

This will create the database with all the default settings (including paths). You may alter any setting you like later on.

like image 20
Mithrandir Avatar answered Oct 18 '22 20:10

Mithrandir