简体   繁体   中英

How to switch database context to newly created database?

I wrote a script to create a database:

1: USE master;
2: IF (db_id('myDB') is null)
3: CREATE DATABASE myDB;
4: USE myDB;

but it does not work... I got an error: Could not locate entry in sysdatabases for database 'myDB'. No entry found with that name. Make sure that the name is entered correctly. (Msg 911 )

Where is my mistake?

Thanks.

ANSWER: go go go
USEFUL LINK: Without using GO, programmatically, you would need to make 2 separate database calls.

在第3行之后添加GO语句 。这将强制执行先前的脚本。

It works if you separate your statements with a go

USE master;
go
IF (db_id('myDB') is null)
  CREATE DATABASE myDB;
go  
USE myDB; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM