How to add ID dynamically to an SQL Query

You have to use the “ROW_NUMBER() OVER” command to give ids to the records you have in your query.

A full command including “ROW_NUMBER OVER” is as the following:

 SELECT ROW_NUMBER() OVER (ORDER BY Column1 ASC) AS RowNo,
 Column1, Column2, Column3, Column4 FROM Table1

RowNo will be your ID here from 1 to the number of records. The output will be like this:

RowNo    Column1    Column3    Column4
1                 C1                 C2                  C3
2                Ca                  Cb                  Cc

And so on….

Popularity: 1% [?]

Tags: , ,

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>