Sujeet Raman

Sujeet Raman

  • 861
  • 927
  • 354.1k

how to check a table is empty and if it is empty drop table?

Sep 4 2020 2:27 PM
I am trying to check a table is empty and if it is empty drop the table in dynamic sp but my query is not working.2 methods i have tried
 
I  am checking if a coloumn is null or empty will drop the table
  1. set @dropempty='IF EXISTS (SELECT * FROM ' + @table' WHERE city IS NULL OR country= '' '' ) DROP TABLE ' + @table  
  2. select @dropempty  
  3. EXEC sys.sp_executesql @dropempty  
2nd methods row check
  1. set @dropempty='IF EXISTS (SELECT 1 FROM ' + @table''') DROP TABLE ' + @table+''  
  2. EXEC sys.sp_executesql @dropempty

Answers (5)