Solved

T-SQL Drop Tables

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

Accepted Solution

by:
becraig earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Try to remove the 3 If statements, and only put the 3 drop lines instead.
0
 

Author Comment

by:Mark01
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now