Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

T-SQL Drop Tables

Posted on 2014-04-06
4
Medium Priority
?
480 Views
Last Modified: 2014-04-07
I’m trying to write T-SQL commands that will drop three tables in a database. I’m running SQL Server 2008 Release 2. After executing each of the commands, I received a “Command(s) completed successfully.” message. However, none of the tables were dropped. Screenshots of the database diagram and tables are attached.

What am I doing wrong?

T-SQL Commands
IF OBJECT_ID('dbo.db_1', 'U') IS NOT NULL
DROP TABLE dbo.tblDurations

IF OBJECT_ID('dbo.db_1', 'U') IS NOT NULL
DROP TABLE dbo.tblObjects

IF OBJECT_ID('dbo.db_1', 'U') IS NOT NULL
DROP TABLE dbo.tblTimes

Open in new window


DATABASE DIAGRAM
Database Diagram
TABLES
Durations TableObjects TableTimes Table
0
Comment
Question by:Mark01
4 Comments
 
LVL 29

Accepted Solution

by:
becraig earned 2000 total points
ID: 39982005
IF OBJECT_ID('dbo.tblDurations', 'U') IS NOT NULL
DROP TABLE dbo.tblDurations

IF OBJECT_ID('dbo.tblObjects', 'U') IS NOT NULL
DROP TABLE dbo.tblObjects

IF OBJECT_ID('dbo.tblTimes', 'U') IS NOT NULL
DROP TABLE dbo.tblTimes
                                  

Open in new window



I think the above might be what you are after ?
If the actual table exists then drop the table.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39982007
why are you using this?

IF OBJECT_ID('dbo.db_1', 'U') IS NOT NULL

what relevance does dbo.db_1 have to [tblDurations] or [tblObjects] or [tblTimes]?

look back over that code and change the IF statements to be relevant for each table
0
 
LVL 11

Expert Comment

by:HuaMinChen
ID: 39982010
Try to remove the 3 If statements, and only put the 3 drop lines instead.
0
 

Author Comment

by:Mark01
ID: 39984218
I executed the command shown below and it returned the error shown below. becraig's commands worked.

COMMAND:
DROP TABLE dbo.tblDurations


ERROR:
Msg 3726, Level 16, State 1, Line 1
Could not drop object 'dbo.tblDurations' because it is referenced by a FOREIGN KEY constraint.

Thank you, becraig, HuaMinChen and PortletPaul.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

971 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