variable
variable

Reputation: 9654

How to create a SQL database and use it in same script?

This is my script:

CREATE DATABASE mydbname;
USE mydbname;

If I run 1st line, then 2nd line, it works OK.

If I run both together then I get this error:

Msg 911, Level 16, State 1, Line 3
Database 'mydbname' does not exist. Make sure that the name is entered correctly.

What is the correct way to write this script?

Upvotes: 0

Views: 126

Answers (1)

diiN__________
diiN__________

Reputation: 7656

Use GO inbetween:

CREATE DATABASE mydbname;
GO
USE mydbname;

Upvotes: 2

Related Questions