Solved

MS-SQL: Delete by Rowcount, leave n- rows???

Posted on 2004-10-01
3
195 Views
Last Modified: 2012-06-21

Q. How can  I delete all but 50 of the most recent rows in a table???
0
Comment
Question by:kvnsdr
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
ShogunWade earned 125 total points
ID: 12201094
it depends on your table schema.


if say you have a seeded identity:

DELETE MyTable WHERE ID NOT IN (SELECT TOP 50 ID FROM MyTable ORDER BY ID DESC)
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12201175
It worked, here's my code..........

DELETE table1 WHERE PriKey NOT IN (SELECT TOP 50 PriKey FROM table1 ORDER BY PriKey DESC)
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 12201192
cool
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now