Thursday, June 20, 2013

RowNumber in Select Query in Sql Server

==>> In this article I am posting how we can fatch record from sql with Row Number this is common issue with many developer . It is really very simple So first i select the whole EMP  table




SELECT * FROM EMP




We use in built function ROW_NUMBER  for get RowID in select Command so see how we apply ROW_NUMBER in select command and get row number on EMP Table : -


 SELECT ROW_NUMBER() OVER (ORDER BY EID DESC) AS 'RowID',* FROM dbo.EMP



Now you can see first column RowID add in the result.now we perform many operations on on this table basis on RowID .

 Now we try to fetch the record according to RowID column that the benefits of ROW_NUMBER Column

SELECT  B.RowID ,
        A.*
FROM    dbo.EMP A
        INNER JOIN ( SELECT ROW_NUMBER() OVER ( ORDER BY EID DESC ) AS 'RowID' ,
                            *
                     FROM   dbo.EMP
                   ) B ON A.EID = B.EID
                          AND B.RowID BETWEEN 5 AND 8
ORDER BY RowID






Now you can see the record from RowID 5 to 8 showing below .



Hope this post would be helpful.  Keep Reading.

No comments:

Post a Comment