Monday, September 22, 2014

SQL Delete All Records Except Top N Records

SQL : Delete All Records Except Top N Records 

-- Test Count Before Delete  :

Select count(Id) from [MyTable]

-- Delete All Records Except Top N Records :

DELETE FROM [MyTable] WHERE [Id] NOT IN (SELECT TOP 500 [Id] FROM [MyTable])

-- Test Count After Delete
Select count(Id) from [MyTable]

No comments: