Solved

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

Posted on 2004-10-01
3
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

688 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