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
- set @dropempty='IF EXISTS (SELECT * FROM ' + @table+ ' WHERE city IS NULL OR country= '' '' ) DROP TABLE ' + @table
- select @dropempty
- EXEC sys.sp_executesql @dropempty
2nd methods row check
- set @dropempty='IF EXISTS (SELECT 1 FROM ' + @table+ ''') DROP TABLE ' + @table+''
- EXEC sys.sp_executesql @dropempty

Pankajkumar PatelRank #181 · 13.4K reputationPosted Sep 7, 2020, 5:28 AM EDT
Nisha RegilRank #1,956 · 748 reputationPosted Sep 4, 2020, 4:29 PM EDT
Sujeet RamanRank #1,329 · 1.3K reputationPosted Sep 4, 2020, 6:48 PM EDT
EXEC sys.sp_executesql @dropempty
Nisha RegilRank #1,956 · 748 reputationPosted Sep 4, 2020, 6:58 PM EDT
Laxmidhar SahooRank #383 · 6.6K reputationPosted Sep 5, 2020, 3:22 AM EDT