September 6, 2010 at 3:35 am
i have a qurey which i am creating joining 4 tables by join .
now when i get resultset i want a new column to add like Srno(serial no.)
which will display the numbers for each row, as if it behaves like an identity column. how to add this column to my already existing query.
September 6, 2010 at 3:40 am
ROW_NUMBER Function will do the trick for you..
HEre are some articles on that
ROW_NUMBER(): An Efficient Alternative to Subqueries
September 6, 2010 at 3:42 am
A small example:
;WITH Tens AS
(
SELECT 1 N UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1
)
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 0)) RN FROM Tens
September 7, 2010 at 7:09 am
See also what you can do with row_number() function
Failing to plan is Planning to fail
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy