How would you assign a variable the total number of rows a table has in SQL Server
?
Asked
Active
Viewed 2.3k times
7

Jon Schneider
- 25,758
- 23
- 142
- 170

edgarmtze
- 24,683
- 80
- 235
- 386
-
Where is the variable in? Your client? SQL Server? – Oded May 05 '11 at 19:05
-
So, on the server itself, not the application? – Oded May 05 '11 at 19:06
-
I believe you have asked the same question before: http://stackoverflow.com/questions/5199412/number-of-rows-sql-server – reggie May 05 '11 at 19:08
-
Well, here I am asking how to assign that number to a variable, other question was how to do that efficiently – edgarmtze May 05 '11 at 19:09
-
@darkcminor, I agree with @reggie, the previous question already assigned the result to a variable. – Brent D May 09 '11 at 19:57
2 Answers
18
Something like the following should do the trick:
Declare @VariableName int
Select @VariableName=count(1) from TableName
-
2If you post code, XML or data samples, **please** highlight those lines in the text editor and click on the "code samples" button ( `{ }` ) on the editor toolbar to nicely format and syntax highlight it! – marc_s May 05 '11 at 20:30
-
3@marc_s Thanks, still trying to figure out the simple things in life! :) – Brent D May 09 '11 at 19:53
0
SELECT COUNT(*) FROM Table
For more details, please provide more detail.

SLaks
- 868,454
- 176
- 1,908
- 1,964