7

How would you assign a variable the total number of rows a table has in SQL Server?

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
edgarmtze
  • 24,683
  • 80
  • 235
  • 386

2 Answers2

18

Something like the following should do the trick:

Declare @VariableName int

Select @VariableName=count(1) from TableName
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Brent D
  • 898
  • 5
  • 16
  • 2
    If 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