Solved

T-SQL Drop Tables

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now