DROP DATABASE if database exists (SQLServer)

Nov 21, 2009 at 12:46 PM

If I start CreateDB.sql and database already exists and used (connections to database is opened) database not droped

Solution could be add additional logic 

CreateDb.sql LINE:10

 

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'Chinook')
BEGIN 
 ALTER DATABASE [Chinook] 
SET OFFLINE 
WITH ROLLBACK IMMEDIATE;
 ALTER DATABASE [Chinook]
  SET ONLINE;  
  
 DROP DATABASE [Chinook]; 
END 
CREATE DATABASE [Chinook];

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'Chinook')

BEGIN 

--Take database offline ignoring any connection made

 ALTER DATABASE [Chinook] 

SET OFFLINE 

WITH ROLLBACK IMMEDIATE;

 

--Bring online before delete 

 ALTER DATABASE [Chinook]

  SET ONLINE;  

 

 DROP DATABASE [Chinook]; 

END 

CREATE DATABASE [Chinook];

 

Coordinator
Mar 30, 2010 at 9:44 PM

Thanks for the suggestion. I will fix it.

 

Coordinator
Mar 30, 2010 at 9:45 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Nov 10, 2010 at 7:59 AM

 

This request has been implemented in the latest Release 1.2.