Home > Software engineering >  Selecting COUNT of a table along with selecting itself
Selecting COUNT of a table along with selecting itself

Time:06-26

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

CodePudding user response:

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

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

Here's the documentation.

  • Related