How do I switch to a SQL Server Server Database that will exist after another command?

Posted by Jason Young on Stack Overflow See other posts from Stack Overflow or by Jason Young
Published on 2010-04-28T19:20:13Z Indexed on 2010/04/28 19:27 UTC
Read the original article Hit count: 333

Filed under:
|
|

I can't get this script to run, because SQL management studio 2008 says the table "NewName" does not exist. However, the script's purpose is to rename an existing database, so that it does exist when it gets to that 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!

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2008