This query will give you the row_number,
SELECT
(SELECT COUNT(*) FROM @table t2 WHERE t2.field <= t1.field) AS row_number,
field,
otherField
FROM @table t1
but there are some restrictions when you want to use it. You have to have one column in your table (in the example it is field
) which is unique and numeric and you can use it as a reference. For example:
DECLARE @table TABLE
(
field INT,
otherField VARCHAR(10)
)
INSERT INTO @table(field,otherField) VALUES (1,'a')
INSERT INTO @table(field,otherField) VALUES (4,'b')
INSERT INTO @table(field,otherField) VALUES (6,'c')
INSERT INTO @table(field,otherField) VALUES (7,'d')
SELECT * FROM @table
returns
field | otherField
------------------
1 | a
4 | b
6 | c
7 | d
and
SELECT
(SELECT COUNT(*) FROM @table t2 WHERE t2.field <= t1.field) AS row_number,
field,
otherField
FROM @table t1
returns
row_number | field | otherField
-------------------------------
1 | 1 | a
2 | 4 | b
3 | 6 | c
4 | 7 | d
This is the solution without functions and stored procedures, but as I said there are the restrictions. But anyway, maybe it is enough for you.