Dynamically counting table rows in T-SQL