Tuesday, September 11, 2007

SQL: ROW_NUMBER()

Last time I encountered problem on my ASP.NET web application because I need to optimize the response time of the paging of my Gridview displaying 18000+ records from the database. While researching for good solutions, I came across with this SQL statement ROW_NUMBER().

Definition:Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
Syntax: ROW_NUMBER() OVER([-partition_by_clause-] -order_by_clause-)
Arguments:"partition_by_clause" - Divides the result set produced by the FROM clause into partitions to which the ROW_NUMBER function is applied. For the syntax of PARTITION BY, see OVER Clause (Transact-SQL).
"order_by_clause" - Determines the order in which the ROW_NUMBER value is assigned to the rows in a partition. For more information, see ORDER BY Clause (Transact-SQL). An integer cannot represent a column when the is used in a ranking function.

Return Type: bigint

Remarks:The ORDER BY clause determines the sequence in which the rows are assigned their unique ROW_NUMBER within a specified partition.

Note:
The ORDER BY in the OVER clause orders ROW_NUMBER. If you add an ORDER BY clause to the SELECT statement that orders by a column(s) other than 'Row Number' the result set will be ordered by the outer ORDER BY.
For examples regarding the usage of this statement, you can refer to this link: http://msdn2.microsoft.com/en-us/library/ms186734.aspx

The sad thing on my end is that I cannot use this as part of my solution since it is only comatible with MS SQL Server 2005 and I am using the 2000 version. :(

No comments: