Today I had an interesting dilema – needed to associate a row number with each row in a record set that I needed to retrieve from a sql table. In other words, if I have 15 rows in the record set, I needed to be able to get the record id of the 14th row. Searching through google I stumbled on ROW_NUMBER() function in sql server. The function is supported for sql server 2005 and up. That’s all it took to get the job done:


SELECT MyRandomTableID, Name, ROW_NUMBER() OVER (ORDER BY Name) AS RowNumber
  FROM MyRandomTable
 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Set your Twitter account name in your settings to use the TwitterBar Section.