Dynamic query in Stored Procedure over table values
SELECT COUNT(*)
FROM Slots s, TempSlots t
WHERE (s.starttime &lt; t.endtime AND t.starttime &lt; s.endtime)
   OR (s.starttime &gt; t.endtime AND t.starttime &gt; s.endtime)


This query counts the overlapping pairs (one record from Slots overlapped one record from TempSlots) only.

To increase the performance you may try to divide this query by two UNIONed queries escaping OR.