.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to get a last inserted record or Row from a table without using any Id column?

Posted By:Syed Shakeer Hussain       Posted Date: March 16, 2010    Points: 10    Category: SQL Query    URL: http://www.dotnetspark.com  

How to get a last inserted record or Row from a table without using primary key,id,TimeStamp and GetData().

Below is my data which is present in a table:

insert into dupemp(name,salary,deptno) values('1sd',4000,12)
insert into dupemp(name,salary,deptno) values('2sd',4000,12)
insert into dupemp(name,salary,deptno) values('3sd',4000,12)
insert into dupemp(name,salary,deptno) values('6sd',4000,12)
insert into dupemp(name,salary,deptno) values('4sd',4000,12)


Method I:-

select top 1 column_list  FROM
(select ROW_NUMBER() over(order by (select 0)) as rownum,column_list from table_name) t order by rownum desc

Method II:-

with CTE as(select *,ROW_NUMBER() over(order by (select 0))as row from dupemp)
select * from cte where row=(select max(row) from cte)


name  salary  deptno

4sd     4000    12


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend