Solved

T-SQL Drop Tables

Posted on 2014-04-06
4
464 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 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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 ClickHouse performs in a general analytical workload using the star schema benchmark test.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

623 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