Tuesday, May 4, 2010

How to get a column from the last record inserted in SQL

I was in real need for getting a particular column value for the last record inserted. After running through many hints provided in the internet, I came up with the following solution:

Used this forum as a starting point:

If there is a column called login in the table tb_admin, to select the value of a particular column of the last row inserted, this is the query :

SELECT login from (select top 1 * from tb_admin order by recid desc) as tempTable

Here "top 1 *" will give the first record and order by recid desc will order it in the descending order to get the last record.

No comments: