SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DELETE FIRST 1000 ROWS


DELETE FIRST 1000 ROWS

Author
Message
shashianireddy
shashianireddy
SSC Journeyman
SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)

Group: General Forum Members
Points: 86 Visits: 135
I HAVE TABLE LIKE BELOW 3000 ROWS

NAME ADDRESS IDNO EMPCODE
SVR HYD 123456 9876
SGH HSD 98765 987654
.
.
.
.


I WANT DELETE FIRST 1000 ROWS IN SQL TABLE ............................

PLZ WRITE QUARIE ..........
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40800 Visits: 38567
shashianireddy (3/29/2014)
I HAVE TABLE LIKE BELOW 3000 ROWS

NAME ADDRESS IDNO EMPCODE
SVR HYD 123456 9876
SGH HSD 98765 987654
.
.
.
.


I WANT DELETE FIRST 1000 ROWS IN SQL TABLE ............................

PLZ WRITE QUARIE ..........


In SQL Server there really is no concept of first 1000 rows. So first question, how are you going to determine what are the first 1000 rows? By what column are you going to order the data and are you ordering it in ascending or descending order?

Here is a start:

DELETE TOP (1000) FROM MyTable ORDER BY yourOrderingColumn

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
gstarsaini
gstarsaini
SSC-Addicted
SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)

Group: General Forum Members
Points: 464 Visits: 182
You can use row number () to create ids for all the rows and then based on any column order you can delete 1000 rows:

DELETE FROM
(SELECT ROW_NUMBER()
OVER (ORDER BY Empcode) AS Row,
Name, Address, Idno
FROM Table_name) AS tablename
WHERE Row<=1000
GilaMonster
GilaMonster
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90273 Visits: 45284
Lynn Pettis (3/29/2014)

DELETE TOP (1000) FROM MyTable ORDER BY yourOrderingColumn


Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'ORDER'.


Amusingly enough, while updates and deletes can take a TOP, they can't have an ORDER BY clause.

To do a delete of the first x rows, ordered by something, this is what's needed:
DELETE FROM MyTable WHERE UniqueColumn IN (SELECT TOP(n) UniqueColumn FROM MyTable ORDER BY ColumnWhichDeterminesOrder)

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40800 Visits: 38567
GilaMonster (3/31/2014)
Lynn Pettis (3/29/2014)

DELETE TOP (1000) FROM MyTable ORDER BY yourOrderingColumn


Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'ORDER'.


Amusingly enough, while updates and deletes can take a TOP, they can't have an ORDER BY clause.

To do a delete of the first x rows, ordered by something, this is what's needed:
DELETE FROM MyTable WHERE UniqueColumn IN (SELECT TOP(n) UniqueColumn FROM MyTable ORDER BY ColumnWhichDeterminesOrder)


Thanks, don't know what I was thinking. Must be these 7 x 12+ hour days, not always thinking straight toward the end of the day.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15678 Visits: 18626
This can be done using sub-query

Using TOP
DECLARE @BATCH_SIZE INT = 10;
DELETE X
FROM (
SELECT TOP (@BATCH_SIZE) [COLUMN]
FROM [TABLE_NAME] M
ORDER BY [COLUMN] DESC
) AS X




Using OFFSET-FETCH (2012)
DECLARE @BATCH_SIZE INT = 10;
DELETE X
FROM (
SELECT [COLUMN]
FROM [TABLE_NAME] M
ORDER BY [COLUMN] DESC
OFFSET 0 ROWS
FETCH FIRST (@BATCH_SIZE) ROWS ONLY
) AS X


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89443 Visits: 41144
While some of the methods posted are ok, there's still the fundamental problem...

shashianireddy (3/29/2014)
I WANT DELETE FIRST 1000 ROWS IN SQL TABLE ............................


... of not knowing what the first 1000 rows should be.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15678 Visits: 18626
Jeff Moden (3/31/2014)
While some of the methods posted are ok, there's still the fundamental problem...

shashianireddy (3/29/2014)
I WANT DELETE FIRST 1000 ROWS IN SQL TABLE ............................


... of not knowing what the first 1000 rows should be.


ooops Blush (thundering voice: read the whole question....;-)
Ed Wagner
Ed Wagner
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17087 Visits: 10094
Once you determine what determines the first rows, I like using this approach:

WITH cte AS (
SELECT TOP 1000
FROM TableName
ORDER BY EntryDate ASC)
DELETE FROM cte;



This gives the added benefit of being able to replace the DELETE with a SELECT so you can see the rows that are going to be deleted before you actually delete them. However, the fundamental question of the order remains. Since the ONLY way to guarantee the order is by using an ORDER BY clause, don't skip this step.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search