[sql] How do I get row id of a row in sql server

I have one table CSBCA1_5_FPCIC_2012_EES207201222743, having two columns employee_id and employee_name

I have used following query

SELECT ROW_NUMBER() OVER (ORDER BY EMPLOYEE_ID) AS ID, EMPLOYEE_ID,EMPLOYEE_NAME 
FROM CSBCA1_5_FPCIC_2012_EES207201222743 

But, it returns the rows in ascending order of employee_id, but I need the rows in order they were inserted into the table.

This question is related to sql sql-server-2005 rowid

The answer is


There is a pseudocolumn called %%physloc%% that shows the physical address of the row.

See Equivalent of Oracle's RowID in SQL Server


SQL does not do that. The order of the tuples in the table are not ordered by insertion date. A lot of people include a column that stores that date of insertion in order to get around this issue.