• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

Add column with number of row

I would like to add a column with the number of the row, i.e. the first row is 1, second is 2, third 3 etc.
How can I do that?
1 Solution
Use ROW_NUMBER() OVER(YourColumn)

Kevin CrossChief Technology OfficerCommented:
I agree with Greg. For some clarity, please see this article:

Please credit Greg for the solution, but you would first need to generate the row number based on some column that identifies your row as first in sort -- syntax is more precisely:
ROW_NUMBER() OVER(ORDER BY your_sort_column) RN

Open in new window

what you may be asking for though is to add an IDENTITY column. If one is not already added, you can do this: ALTER TABLE YourTable ADD ID BIGINT IDENTITY(1,1);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now