Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I'm trying to delete all the rows that are behind the top 5 rows.

SQL
CREATE PROCEDURE DeleteOtherNotes

AS
DELETE FROM notes
WHERE notes_id NOT IN
(SELECT notes_id
FROM
(SELECT notes_id FROM notes ORDER BY notes_id DESC)
foo);


In which, of course, notes is the table, and notes_id is the auto numbered id of that table.

Problem is, i'll get this message:

Msg 1033, Level 15, State 1, Procedure DeleteOtherAantekeningen, Line 8<br />
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.


What can i do about this?
Posted

1 solution

SQL
DELETE FROM notes WHERE (notes_id NOT IN (SELECT TOP 5 notes_id FROM notes))
 
Share this answer
 
Comments
Amateurgrammer 22-Mar-11 11:43am    
After copying the 'ORDER BY notes_id DESC' behind your code,i've gotten the desired results,
Many thanks.
Eduard Keilholz 22-Mar-11 15:56pm    
Great!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900