?
Solved

DROP TABLE IF EMPTY DATA

Posted on 2011-03-15
6
Medium Priority
?
350 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 2000 total points
ID: 35136336
if not exists (select top 1 * from [Table]) DROP TABLE [Table]
0
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!

 
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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

765 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