Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

delete top 1000 rows from table Expand / Collapse
Author
Message
Posted Monday, November 05, 2012 9:38 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, May 10, 2013 2:47 PM
Points: 83, Visits: 244
Hello

I need to delete 1000 rows from table and it should do it batch wise.
means
i need to loop delete statement that delete top 1000 rows.

please help me
Post #1381169
Posted Monday, November 05, 2012 9:45 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 8,620, Visits: 8,261
yogi123 (11/5/2012)
Hello

I need to delete 1000 rows from table and it should do it batch wise.
means
i need to loop delete statement that delete top 1000 rows.

please help me


Why do you think you need to loop to delete the top 1,000 rows?

You didn't provide any details so it is impossible to provide much help.

If you need to use batches to delete rows from a table you can use something like this.

while @@ROWCOUNT > 0
delete top(1000)
from YourTable
Where ???



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1381173
Posted Wednesday, November 07, 2012 10:16 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 7:57 PM
Points: 6,998, Visits: 13,949
Sean Lange (11/5/2012)
yogi123 (11/5/2012)
Hello

I need to delete 1000 rows from table and it should do it batch wise.
means
i need to loop delete statement that delete top 1000 rows.

please help me


Why do you think you need to loop to delete the top 1,000 rows?

You didn't provide any details so it is impossible to provide much help.

If you need to use batches to delete rows from a table you can use something like this.

while @@ROWCOUNT > 0
delete top(1000)
from YourTable
Where ???



Also - since there's no physical order in a DB, there had better be a good predictable ORDER BY clause added in to define how the TOP 1000 is supposed to find said 1000.


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #1382074
Posted Wednesday, November 07, 2012 10:27 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 8,620, Visits: 8,261
Matt Miller (#4) (11/7/2012)
Sean Lange (11/5/2012)
yogi123 (11/5/2012)
Hello

I need to delete 1000 rows from table and it should do it batch wise.
means
i need to loop delete statement that delete top 1000 rows.

please help me


Why do you think you need to loop to delete the top 1,000 rows?

You didn't provide any details so it is impossible to provide much help.

If you need to use batches to delete rows from a table you can use something like this.

while @@ROWCOUNT > 0
delete top(1000)
from YourTable
Where ???



Also - since there's no physical order in a DB, there had better be a good predictable ORDER BY clause added in to define how the TOP 1000 is supposed to find said 1000.


It actually really doesn't make any difference here. The intention is to delete rows in a batch. Why not let sql decide which rows are the top? It will determine the top based on whatever it finds as the easiest way to get them. It doesn't make any difference the order they are deleted as long as at the end they are all deleted in the end.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1382077
Posted Wednesday, November 07, 2012 10:56 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 7:57 PM
Points: 6,998, Visits: 13,949
Sean Lange (11/7/2012)
Matt Miller (#4) (11/7/2012)
Sean Lange (11/5/2012)
yogi123 (11/5/2012)
Hello

I need to delete 1000 rows from table and it should do it batch wise.
means
i need to loop delete statement that delete top 1000 rows.

please help me


Why do you think you need to loop to delete the top 1,000 rows?

You didn't provide any details so it is impossible to provide much help.

If you need to use batches to delete rows from a table you can use something like this.

while @@ROWCOUNT > 0
delete top(1000)
from YourTable
Where ???



Also - since there's no physical order in a DB, there had better be a good predictable ORDER BY clause added in to define how the TOP 1000 is supposed to find said 1000.


It actually really doesn't make any difference here. The intention is to delete rows in a batch. Why not let sql decide which rows are the top? It will determine the top based on whatever it finds as the easiest way to get them. It doesn't make any difference the order they are deleted as long as at the end they are all deleted in the end.


Well if you can align the ORDER BY to the CI, the delete won't have to revisit the same pages multiple times which might help improve writes. That said - if the intent is to keep cycling to run this across the entire table (instead of just doing the top 1000 once and then stopping), it likely won't make much of a difference.


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #1382091
Posted Wednesday, November 07, 2012 12:35 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 8,620, Visits: 8,261
Matt Miller (#4) (11/7/2012)
Sean Lange (11/7/2012)
Matt Miller (#4) (11/7/2012)
Sean Lange (11/5/2012)
yogi123 (11/5/2012)
Hello

I need to delete 1000 rows from table and it should do it batch wise.
means
i need to loop delete statement that delete top 1000 rows.

please help me


Why do you think you need to loop to delete the top 1,000 rows?

You didn't provide any details so it is impossible to provide much help.

If you need to use batches to delete rows from a table you can use something like this.

while @@ROWCOUNT > 0
delete top(1000)
from YourTable
Where ???



Also - since there's no physical order in a DB, there had better be a good predictable ORDER BY clause added in to define how the TOP 1000 is supposed to find said 1000.


It actually really doesn't make any difference here. The intention is to delete rows in a batch. Why not let sql decide which rows are the top? It will determine the top based on whatever it finds as the easiest way to get them. It doesn't make any difference the order they are deleted as long as at the end they are all deleted in the end.


Well if you can align the ORDER BY to the CI, the delete won't have to revisit the same pages multiple times which might help improve writes. That said - if the intent is to keep cycling to run this across the entire table (instead of just doing the top 1000 once and then stopping), it likely won't make much of a difference.


Good point about the CI, didn't think about that. I would hazard a guess that with no order by on something like this it would just delete the rows in page sequence. Certainly if we just wanted to delete the top x number of rows we need an order by of some sort.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1382133
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse