• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

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.
0
emi_sastra
Asked:
emi_sastra
1 Solution
 
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
DECLARE @count int
SELECT @count = Count(1) from <tablename>

IF @count = 0
BEGIN
    DROP TABLE <tablename>
END
0
 
Roman GhermanCommented:
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
 
EvilPostItCommented:
if not exists (select top 1 * from [Table]) DROP TABLE [Table]
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
Roman GhermanCommented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now