Mateen Bagheri
Mateen Bagheri

Reputation: 99

Selecting COUNT of a table along with selecting itself

Lets say I have an INT variable @length set to number of rows in my result table in procedure. How can I set this without selecting my data twice? What I do currently is this:

SELECT [data] FROM [mytable]
SET @length = (SELECT COUNT(*) FROM [mytable] WHERE [Condition])

However since my conditions are complicated and number of data is fairly a lot I feel like this is not optimal.

I could create a temp table and select my data into it and count length of it which seems to be a better approach but I wanted to know if there are any better solutions. thanks for the answers

Upvotes: 0

Views: 41

Answers (1)

George Menoutis
George Menoutis

Reputation: 7250

just use @@rowcount immediately after you select to return last operation rowcount:

select [data] from [mytable]
set @length=@@rowcount

Here's the documentation.

Upvotes: 1

Related Questions