Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sqlcmd to create a database

Tags:

I have a .sql script and I want to build a database from it. How to do it in sqlcmd? I know it's like:

CREATE DATABASE dbName
GO

But how to specify the .sql script and location to build the database?

like image 455
Cobold Avatar asked Jan 01 '12 14:01

Cobold


People also ask

How do I create a new SQL database?

Use SQL Server Management StudioRight-click Databases, and then select New Database. In New Database, enter a database name. To create the database by accepting all default values, select OK; otherwise, continue with the following optional steps. To change the owner name, select (...) to select another owner.


2 Answers

Use @Jeremiah Peschka's answer to supply the sqlcmd utility with the script to execute.

As for the location for the newly created database, it can be specified as part of the CREATE DATABASE command:

CREATE DATABASE dbName
ON (
  NAME = dbName_dat,
  FILENAME = 'D:\path\to\dbName.mdf'
)
LOG ON (
  NAME = dbName_log,
  FILENAME = 'D:\path\to\dbName.ldf'
)

As you can see from the linked article, you can specify other properties as well, like initial size, maximum size etc.

like image 88
Andriy M Avatar answered Sep 20 '22 11:09

Andriy M


This is very simple. Just enter following command to run sqlcmd:
sqlcmd -U sa -P password
Then enter:

CREATE DATABASE MYDB
GO

This will create the db. Then enter "quit" to exit.

like image 26
Badar Avatar answered Sep 22 '22 11:09

Badar