Solved

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

Posted on 2004-10-01
3
194 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
Comment Utility
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
Comment Utility
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
Comment Utility
cool
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

772 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

10 Experts available now in Live!

Get 1:1 Help Now