Solved

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

Posted on 2004-10-01
3
198 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Sum Over Multiple Tables 20 35
How can I retrieve the column names from a T-SQL table? 2 16
Split string into 3 separate fields 5 22
SQL Help 4 16
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

726 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