How to switch to the SQL Server Server database that will exist after another command?

I cannot run this script because SQL 2008 management studio says that the table "NewName" does not exist. However, the purpose of the script is to rename an existing database so that it exists when it falls into this line. Ideas?

Use Master;

ALTER DATABASE OldName SET SINGLE_USER WITH NO_WAIT;
ALTER DATABASE OldName MODIFY NAME = NewName;

ALTER DATABASE NewName SET MULTI_USER;
Use NewName; --THIS LINE FAILS BEFORE THE SCRIPT EVEN RUNS!
+3
source share
1 answer

Add GObetween 2nd and 3rd instructions ALTER DATABASE.

http://msdn.microsoft.com/en-us/library/ms188037.aspx

+7
source

Source: https://habr.com/ru/post/1743242/


All Articles