• LeelaKrishna (7/11/2009)


    In one of stored procedures written ages back which uses SET ROWCOUNT to limit rows effected by DELETE operation after that.. Rows from BOTTOM (Old rows) are getting deleted on one MS SQL 2005 Installation BUT on another MS SQL 2005 installation TOP (latest rows) are getting deleted.. I don't have any clue of what might be cause..

    Tables, by definition, have no order. So if you set RowCount to 200 and issue a delete statement, you're telling SQL to delete 200 rows that meet the conditions in the where clause. You've not saying anything about which 200 rows, that's completely up to SQL.

    If you want to delete the oldest rows, then you need something like this (because order by is not permitted in a delete statement)

    DELETE FROM SomeTable WHERE PrimaryKeyColumn IN (SELECT TOP (200) PrimaryKeycolumn FROM SomeTable ORDER BY SomeDate)

    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