Solved

SQL Script Error - DROP and CREATE

Posted on 2011-03-05
2
176 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
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:
ewangoya 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DateTimepicker 4 33
how to read json value 2 32
Entity Framework 7 33
compare between entities in entity framework 8 12
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

861 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now