How To Delete Top 100 Rows From SQL Server Tables

Posted by Gopinath on Tech Dreams See other posts from Tech Dreams or by Gopinath
Published on Thu, 10 Jun 2010 16:30:00 +0000 Indexed on 2010/06/10 16:33 UTC
Read the original article Hit count: 1405

Filed under:
|
|

If you want to delete top 100/n records from an SQL Server table, it is very easy with the following query:

DELETE FROM MyTable
WHERE PK_Column
IN(
    SELECT TOP 100 PK_Column
    FROM MyTable
    ORDER BY creation
   )

Why Would You Require To Delete Top 100 Records?

I often delete a top n records of a table when number of rows in the are too huge. Lets say if I’ve 1000000000 records in a table, deleting 10000 rows at a time in a loop is faster than trying to delete all the 1000000000  at a time.

What ever may be reason, if you ever come across a requirement of deleting a bunch of rows at a time, this query will be helpful to you.

Join us on Facebook to read all our stories right inside your Facebook news feed.


© Tech Dreams or respective owner

Related posts about Microsoft

Related posts about SQL Server