Solved

SQL Script Error - DROP and CREATE

Posted on 2011-03-05
2
181 Views
Last Modified: 2012-08-14
I am having a problem with a simple SQL Server script.  For some reason, after I drop the PIMTool database it does not get created.  The "IF not EXISTS (Sel....*" on line 5 is not getting executed.  Because the PIMTool DB does not created I get an error message on line 8 on the "Use PIMTool".  ANy suggestions?

use Master;
if exists (SELECT name FROM Master.dbo.sysdatabases WHERE name = N'PIMTool')
drop DATABASE [PIMTool];

IF not EXISTS (SELECT name FROM Master.dbo.sysdatabases WHERE name = N'PIMTool')
CREATE DATABASE [PIMTool];

use PIMTool;

Server: Msg 911, Level 16, State 1, Line 8
Could not locate entry in sysdatabases for database 'PIMTool'. No entry found with that name. Make sure that the name is entered correctly.
0
Comment
Question by:Hojoformo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Expert Comment

by:LIONKING
ID: 35044197
I don't think you can create the database and use it (with a "use" statement) in the same script... You can try running your script with an EXEC instruccion and your statement saved in a variable.

Not 100% sure about this, but I tried it and it didn't work for me.
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 35044359
Add GO to your statements


use Master;
if exists (SELECT name FROM Master.dbo.sysdatabases WHERE name = N'PIMTool')
drop DATABASE [PIMTool];
GO

IF not EXISTS (SELECT name FROM Master.dbo.sysdatabases WHERE name = N'PIMTool')
CREATE DATABASE [PIMTool];
GO

use PIMTool;
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question