7

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

Jon Schneider
  • 23,615
  • 19
  • 137
  • 163
edgarmtze
  • 23,987
  • 75
  • 226
  • 373

2 Answers2

18

Something like the following should do the trick:

Declare @VariableName int

Select @VariableName=count(1) from TableName
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
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
  • 837,282
  • 173
  • 1,862
  • 1,933