devxlogo

A Fast Way to Check if a Table Is Empty

A Fast Way to Check if a Table Is Empty

If all you need to know is whether a table is empty, using COUNT(*) is a bad alternative, because if the table is very large, it will take some time to count all the rows. Instead, use a SQL statement like the following:

 SELECT 'The table is NOT empty' WHERE EXISTS (SELECT 1 FROM TheTable)

Since the where-clause uses EXISTS, SQL Server will stop processing as soon as it encounters a row in TheTable, and you will know that TheTable is not empty.

devx-admin

Share the Post: