views:

134

answers:

2

Is there a fast/efficiency way to check if a table is empty?

DECLARE @StartEndTimes TABLE
(
    id bigint,
    StartTime datetime,
    EndTime datetime
)

IF @StartEndTimes IS NOT NULL
+1  A: 

I think your best bet might be COUNT

DECLARE @StartEndTimes TABLE 
( 
    id bigint, 
    StartTime datetime, 
    EndTime datetime 
)

SELECT COUNT(1) FROM @StartEndTimes
astander
beaten to the punch...+1
kekekela
+3  A: 

Rather than counting you can;

if exists (select id from @StartEndTimes)
   set @has_stuff = 1

Which will return as soon as it hits a row.

Alex K.
+1 - I'd go for EXISTS over COUNT for performance
AdaTheDev
+1 so would I. it looks for a row and returns...count has to traverse the table
gbn