Pages

Tuesday, November 13, 2012

Generate sequence number in SQL Server (ROWNUM like functionality in SQL Server)

How can we generate sequential numeric number for each row - Like ROWNUM in Oracle ? In SQL Server this is possible through Ranking functions. Here is how to do that.
Query:
SELECT ROW_NUMBER() OVER (ORDER BY object_id) SRNo, *  FROM sys.tables ;
In the above query we are using ROW_NUMBER () – a Ranking function in SQL Server which generates the sequence no.
OVER clause – This is mandatory as it defines the dataset (rows) on which sequence no will be generated.
After executing the above query , you will the results as below.

image
In my other upcoming articles , I will cover RANKING functions in detail.

Happy Learning !!!

No comments:

Post a Comment