March 29, 2014 at 2:21 am
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 ..........
March 29, 2014 at 7:56 am
shashianireddy (3/29/2014)
I HAVE TABLE LIKE BELOW 3000 ROWSNAME 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
March 31, 2014 at 1:11 am
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
March 31, 2014 at 2:15 am
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
March 31, 2014 at 5:05 am
GilaMonster (3/31/2014)
Lynn Pettis (3/29/2014)
DELETE TOP (1000) FROM MyTable ORDER BY yourOrderingColumnMsg 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.
March 31, 2014 at 5:33 am
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
March 31, 2014 at 10:25 am
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
Change is inevitable... Change for the better is not.
March 31, 2014 at 11:39 am
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....;-)
March 31, 2014 at 12:03 pm
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.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply