Wednesday, May 30, 2007

Get Number of Row in Select Statement

For SQL Server 2000, you can using below SQL Query or SQL statement to get number of row for a SELECT Statement.

SELECT rank=COUNT(*), A1.countryname, A1.countrycode FROM country A1, country A2 WHERE A1.countryname + A1.countrycode >= A2.countryname + A2.countrycode GROUP BY A1.countryname, A1.countrycode ORDER BY rank


Table (Before using the above SQL Query): SELECT * FROM TABLE



Table (After using the above SQL Query): It add 1 more column to show the number of Row for this table.





For SQL Server 2005, you can using below SQL Query or SQL statement to get number of row for a SELECT Statement.

SELECT rank() OVER (ORDER BY A1.countryname, A1.countrycode) as rank, A1.countryname, A1.countrycode FROM country A1 ORDER BY rank

The result will same as above image.

For more information about SQL Query Syntax or SQL statement Syntax,click here
For more information about Example of SQL Query or SQL statement,click here