How to retrieve record from a specific row for tables that do not have an ID

Taking the example of the Northwind database, the table Customer has a primary key that is non-numeric. So in order to find the row at position 20 (ordered by CustomerID), we will need to do some workarounds to achieve the same. Let us see how we can do this easily by using the
ROW_NUMBER()

Query : To find the record at a particular row

USE NORTHWIND
GO
SELECT *
FROM ( SELECT ROW_NUMBER() OVER(ORDER BY CustomerID) AS cid, CustomerID, CompanyName, ContactName, ContactTitle
FROM Customers) C
WHERE cid = 20


About The Author

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

1 comment:

Madhivanan said...

Also see what you can do with row_number() function

http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

Madhivanan
http://beyondrelational.com/blogs/madhivanan