Solved

T-SQL Drop Tables

Posted on 2014-04-06
4
463 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
[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
4 Comments
 
LVL 29

Accepted Solution

by:
becraig earned 500 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 48

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 10

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

751 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