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

Syedshakeer
Posted by Syedshakeer under Sql Server category on | Views : 5452
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)


Query:-

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)


OutPut:-

name  salary  deptno 

4sd 4000 12

Comments or Responses

Login to post response