SQL Server Admin
T-SQL Articles

April 01, 2008

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


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter



 
  Feedback:

comments

1 Response to "How to retrieve record from a specific row for tables that do not have an ID"
  1. Madhivanan said...
    February 2, 2010 1:18 AM

    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

 

Copyright © 2009-2011 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions