Delete All Rows From a Table Except Top(N) Rows

This script contains all code blocks within TechNet Wiki article T-SQL: Delete All Rows From a Table Except Top(N) Rows written by Saeid Hasani. This article comes from a question  that asked in MSDN forum. The problem is how to delete all rows from a table but remain the Top (N)

 
 
 
 
 
5 Star
(1)
221 times
Add to favorites
Databases
1/3/2015
E-mail Twitter del.icio.us Digg Facebook
Verified on the following platforms
Windows 10 No
Windows Server 2012 Yes
Windows Server 2012 R2 No
Windows Server 2008 R2 Yes
Windows Server 2008 Yes
Windows Server 2003 Yes
Windows 8 Yes
Windows 7 Yes
Windows Vista Yes
Windows XP Yes
Windows 2000 Yes
This script is tested on these platforms by the author. It is likely to work on other platforms as well. If you try it and find that it works on another platform, please add a note to the script discussion to let others know.