DROP TABLE IF EMPTY DATA

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.
LVL 1
emi_sastraAsked:
Who is Participating?
 
EvilPostItConnect With a Mentor Commented:
if not exists (select top 1 * from [Table]) DROP TABLE [Table]
0
 
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
DECLARE @count int
SELECT @count = Count(1) from <tablename>

IF @count = 0
BEGIN
    DROP TABLE <tablename>
END
0
 
Roman GhermanSenior Software EngineerCommented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Roman GhermanSenior Software EngineerCommented:
oopps, yes - forgot to add TOP 1


----------------
You can find me on: http://extremedev.blogspot.com/
0
 
emi_sastraAuthor Commented:
Hi All,

Thank you very much for your solution.

I think EvilPostIt code is the simplest.

Thank you.
0
 
EvilPostItCommented:
You calling me simple ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.