0

I have a script which uses an existing database to create tables and enter values. Now I wish to create the database through this same script and then create the tables and values which are just a copy of another database.

I have:

CREATE DATABASE MyDatabase

USE [MyDatabase]
....Code that creates the tables and values from another database.

The USE [MyDatabase] command is not recognized since MyDatabase does not exist anywhere yet. Is there a way around this?

1 Answer 1

2

Yes, insert the GO command in between your scripts.

CREATE DATABASE MyDatabase
GO
USE [MyDatabase]
Sign up to request clarification or add additional context in comments.

6 Comments

Be careful with this. GO is not part of the SQL language. It works with some tools, because those tools are configured specially to look for it... but they can be set not to look for it, or to look for something else instead.
How do you set the directory in which the database is created in?
CREATE DATABASE has syntax for that. msdn.microsoft.com/en-us/library/ms176061.aspx
@JoelCoehoorn for fun set your batch separator to SELECT or FROM :-)
@AaronBertrand or even better, a co-worker's separator when you catch him away from his desk
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.