Link to home
Start Free TrialLog in
Avatar of sdc248
sdc248Flag for United States of America

asked on

Query help - delete all but 50 rows of data

Hi:

What is the fastest way to delete all but 50 rows  of data from a database table? Thanks.
Avatar of Chris Mangus
Chris Mangus
Flag of United States of America image

Do you care which 50 rows you keep?  If not:

Delete From myTable Where myPK Not In (Select Top (50) myPK From myTable)
ASKER CERTIFIED SOLUTION
Avatar of pai_prasad
pai_prasad
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sdc248

ASKER

Thanks guys. The "truncate table" command helps a lot. The old "delete from mytable where" method took forever to delete data from a couple of huge tables (200 million rows) and eventually failed because the transaction log grew rapidly and fill up the server.  Luckily these huge tables don't have identity columns or foreign keys.