DELETE FIRST 1000 ROWS

  • 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 ..........

  • 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

  • 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

  • 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
  • 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.

  • 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

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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....;-)

  • 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