Solved

DROP TABLE IF EMPTY DATA

Posted on 2011-03-15
6
341 Views
Last Modified: 2012-05-11
Hi All,

I want to drop table if there is no data inside the table.

Usually I use : DROP TABLE TableName

How could I do it ?

Thank you.
0
Comment
Question by:emi_sastra
[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
6 Comments
 
LVL 19

Expert Comment

by:Rikin Shah
ID: 35136293
DECLARE @count int
SELECT @count = Count(1) from <tablename>

IF @count = 0
BEGIN
    DROP TABLE <tablename>
END
0
 
LVL 9

Expert Comment

by:Roman Gherman
ID: 35136320
Or you could do it simpler:

IF (SELECT 1 FROM <tableName>) IS NULL
BEGIN
   DROP TABLE <tableName>
END


----------------
You can find me on: http://extremedev.blogspot.com/
0
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 500 total points
ID: 35136336
if not exists (select top 1 * from [Table]) DROP TABLE [Table]
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 9

Expert Comment

by:Roman Gherman
ID: 35136352
oopps, yes - forgot to add TOP 1


----------------
You can find me on: http://extremedev.blogspot.com/
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35136357
Hi All,

Thank you very much for your solution.

I think EvilPostIt code is the simplest.

Thank you.
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 35136361
You calling me simple ;)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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