views:

601

answers:

6

Hi Friend,

Can somebody please, help me with a query to delete only 500 rows from a table which has 20000 rows. Also has to be older than a particular date.

Thanks for your help,

Soofy

+6  A: 

You can use the Top keyword like you would in a select

Delete Top (500) 
From myTable
Where Date < '01/01/2009'
Jeremy
In MSSQL 2005 "500" needs to be in brackets. Eg. Delete Top (500).
Simon Hartcher
Good catch. Fixed it
Jeremy
otherwise know as parentheses () ... lol brackets []
Chad Grant
@Deviant - http://en.wikipedia.org/wiki/Bracket - They are all brackets. Specifically I could have said round brackets.
Simon Hartcher
i forgot, wikipedia knows all. lol
Chad Grant
Fortunately, SQL Server (and most compilers[other than one you may have written]) knows the difference.
Jeff O
+1  A: 

SET ROWCOUNT 500

DELETE FROM TableName WHERE TheDate < @YourDate

Chad Grant
+6  A: 

If you're using SQL Server 2005, you can do this:

DELETE TOP (500) FROM your_table
WHERE date_field < @my_particular_date

or you can do this:

SET ROWCOUNT 500

DELETE your_table
WHERE date_field < @my_particular_date

in SQL Server 2000, you can do this:

DELETE your_table
WHERE pk_field IN (
  SELECT TOP (500) * FROM your_table
  WHERE date_field < @my_particular_date
)
Nathan DeWitt
With your example of select TOP x, wouldn't you have to specify an order by, and only return the pk_field
Jonathan
@Jonathan, presumably you would specify an `ORDER BY` clause, however the original question wasn't worded that way. They didn't want the oldest 500 records deleted, they just wanted 500 records deleted that *happened to be* older than a particular date.
Nathan DeWitt
A good point, and one which I had overlooked. :)
Jonathan
+3  A: 
DELETE FROM Table_Name WHERE Primary_Key_Column IN (
    SELECT TOP 500 Primary_Key_Column FROM Table_Name WHERE [Date] < '01/01/2009'  ORDER BY Primary_Key_Column ASC
)
M. Jahedbozorgan
that should be [Date] < '01/01/2009' because the orig. poster wants to delete older dates, not newer ones.
Nathan DeWitt
Sorry. I have edited to reflect that. (one down vote for this?)
M. Jahedbozorgan
+1  A: 

Top only works in Transact Sql, each sql has it's own version

For mySql and posGres

Delete 
From myTable
Where Date < '01/01/2009'
LIMIT 10;

For oracle:

SELECT 
FROM myTable 
WHERE Date < '01/01/2009'
and ROWNUM <= 10
e5
orig question was tagged sqlserver, hence the T-SQL specific answers.
Nathan DeWitt
That doesn't negate the validity of these answers and since the question is phrased without concern for SQL flavour it will be helpful if someone needs this in the future.
Michael Prewecki
A: 

The only thing I'll add is you probably want to use "ORDER BY [DATE] DESC" at the end of most of these queries. And if you have multiple matching dates, you'll want to add additional column ordering to get the correct values deleted.

This presumes of course, that you actually have a "createddate" or "modifieddate" you can use to sort by. You didn't specify whether it was the oldest created or the oldest unmodified. (ie if ID=1 is the oldest but I modified it yesterday should it still be deleted?) which you'll need to be careful with if you sort by the primary key - assuming you're using an incrementing primary key and not GUIDs or something...

Edward