Jaggu
Jaggu

Reputation: 6428

How can I get the number of rows in dynamic query's result?

I have created one dynamic query and it all works well. I execute the query using:

EXEC sp_executesql @SQLQuery

where @SQLQuery is one dynamic query.

My only question is how can I also return the number of rows present after the execution of this query? I hope my question is clear.

Thanks in advance:)

Upvotes: 2

Views: 5401

Answers (2)

eronille
eronille

Reputation: 1

use the count() function of SQL EXEC sp_executesql @SQLQuery

DECLARE @rownum as int
SET @rownum = (SELECT count([column]) from [tablename])

Upvotes: -1

Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15433

You can use the @@rowcount which will return you the last query effected row count.

EXEC sp_executesql @SQLQuery

DECLARE @rowcount int
SET @rowcount = @@rowcount
SELECT @rowcount as NumofRows

Upvotes: 5

Related Questions