• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

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


Q. How can  I delete all but 50 of the most recent rows in a table???
0
kvnsdr
Asked:
kvnsdr
  • 2
1 Solution
 
ShogunWadeCommented:
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
 
kvnsdrAuthor Commented:
It worked, here's my code..........

DELETE table1 WHERE PriKey NOT IN (SELECT TOP 50 PriKey FROM table1 ORDER BY PriKey DESC)
0
 
ShogunWadeCommented:
cool
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now