GrayFox
GrayFox

Reputation: 1089

Create Database and it's tables in 1 script

I'm new to SQL and have the follow problem:

I want 1 script where I create my database and its tables:

CREATE DATABASE TestDB;
USE TestDB
CREATE TABLE Customers
(
  CustomerID int NOT NULL,
  Company nvarchar(150) Not NULL,
  CONSTRAINT pk_CustomerID PRIMARY KEY (CustomerID)
);
// further tables with same scheme like 'Customers'

With this script, I always get the error: TestDB is not existing...

I use SQL Server Express 2008 and SQL Server Management Studio

Thanks for help!

Upvotes: 0

Views: 52

Answers (1)

Sparky
Sparky

Reputation: 15085

Try this:

   CREATE DATABASE TestDB;
   GO
   USE TestDB
   GO
    CREATE TABLE Customers
    (
      CustomerID int NOT NULL,
      Company nvarchar(150) Not NULL,
      CONSTRAINT pk_CustomerID PRIMARY KEY (CustomerID)
    );
    // further tables with same scheme like 'Customers'

Upvotes: 1

Related Questions