Sql-server – How to Delete A Database And Log Files With SQL 2008 Script

deletescriptingsql-server-2008

While working on making an automated Delete and Restore script for my backups I ran into a snag. I can not delete the database; however, the delete button works exactly how I need it to. Can anyone tell me the script for that delete button?

SQL Delete button

I have also tried another query to do this task, however it is not working like I would like. Here is the code:

> USE CENTRAL
ALTER DATABASE DATABASE REMOVE FILE DATABASE_1.LDF
ALTER DATABASE DATABASE REMOVE FILE DATABASE_2.LDF
ALTER DATABASE DATABASE REMOVE FILE DATABASE.MDF
ALTER DATABASE DATABASE SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE DATABASE SET OFFLINE
EXEC sp_detach_db DATABASE, true;
DROP DATABASE DATABASE
GO

The error that I get when I run the code is:

Executed as user: NT AUTHORITY\SYSTEM. Incorrect syntax near '.'. [SQLSTATE 42000] (Error 102)  Incorrect syntax near '.'. [SQLSTATE 42000] (Error 102)  Incorrect syntax near '.'. [SQLSTATE 42000] (Error 102).  The step failed.

I am not sure what I am doing wrong, can someone please tell me?

Best Answer

If you click on the 'Script' in the execute box that pops up from the delete button, it reveals that the script actually uses a DROP command:

USE [DATABASE]
GO
/****** Object:  Table [dbo].[TABLE]    Script Date: 3/6/2014 9:29:39 AM ******/
DROP TABLE [dbo].[TABLE]
GO