Click here to Skip to main content
16,013,648 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
ID	DATE
1	2015-07-23 04:00:02.000
2	2015-07-24 04:00:02.000
3	2015-07-25 04:00:02.000
4	2015-07-26 04:00:02.000
5	2015-07-27 04:00:02.000
6	2015-07-28 04:00:02.000
7	2015-07-29 04:00:02.000
8	2015-07-30 04:00:02.000



how do i delete the top 2 date record by using id column please explain me on stored procedure code because i have lot lakhs of records every time i cannot mention date in where condition
Posted
Comments
Tomas Takac 5-Jan-16 3:59am    
In SQL Server you can use TOP with DELETE[^]. Is that your question?
Tryingtobeageek 5-Jan-16 4:05am    
s sir

I have place the above given data into ans table

select top 2 * into #temp from ans order by sdate desc

select * from #temp

delete from ans where id in (select id from #temp)

select * from ans

drop table #temp
 
Share this answer
 
SQL
DELETE from mytable WHERE id IN (SELECT TOP 2 ID FROM mytable ORDER BY Date ASC)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900