In SQL Server 2008, I have an application where users can run queries against a database. I want to prevent them from running a query that will return millions of results and tax system resources. The current solution is wrap whatever query is input in a count(*) function like so:
Select count(*) as COUNT
from (SELECT SOMETHING FROM SOMETABLE) as TMPCOUNT0;
Works fine until the user tries to run COUNT(*)
on their own.
Select count(*) as COUNT
from (SELECT COUNT(*) FROM SOMETABLE) as TMPCOUNT0;
--(should return 1)
However SQL Server does not like that my inner COUNT(*)
column has no name in my derived table and errors out with:
No column name was specified for column 1 of 'TMPCOUNT0'.
I know I could fix this if I were running the query by specifying a name for inner count, but since end users are not aware of this, I'm hoping to find a more elegant solution.
Any ideas?