• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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