Is it possibile to count the number of result that a query returns using sp_executesql without executing query?
What I mean:
I have a procedure that gets a sql query in string.
Example:
SELECT KolumnaA FROM Users WHERE KolumnaA > 5
I would like to assign count of how many results this query will return, and store it in a variable, but I do not want to actually execute the query.
I cannot use this solution:
EXECUTE sp_executesql @sql
SET @allCount = @@rowcount
because it returns the query result, in addition to getting the count of returned rows.
I think there are two parts to the question, which I will address.
The first part of the question is how to return row counts instead of the query results. This is done using Count(item). Using Count(1) instead of Count(KolumnaA) can be slightly faster, since it just counts the number of rows to be returned, instead of retreiving a specific column.
The second part is assigning this to a variable. If you need to use sp_executesql, you can do as follows:
Alternatively, you can try to use the sp_executesql output feature: