Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete only 500 rows from a table

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

like image 461
user70636 Avatar asked Nov 30 '22 12:11

user70636


2 Answers

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

Delete Top (500) 
From myTable
Where Date < '01/01/2009'
like image 63
Jeremy Avatar answered Dec 04 '22 07:12

Jeremy


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
)
like image 29
Nathan DeWitt Avatar answered Dec 04 '22 08:12

Nathan DeWitt