Solved

DROP TABLE IF EMPTY DATA

Posted on 2011-03-15
6
344 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

705 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