Need to delete lots of data, do it in small chunks

By adding a top clause to your delete statement you can delete a chunk of records at a time. By combining it with a while you can put it in a loop and wipe the entire table.

WHILE EXISTS (SELECT * FROM Foo)

BEGIN

DELETE TOP(100) FROM Foo

END

Had someone send me an email telling me that it might be better to use a TRUNCATE TABLE command then this method, and in most cases this is preferable. Conversly not always do us "evil" developers get TRUNCATE permissions from our DBA's. You can read more about TRUNCATE here (http://msdn2.microsoft.com/en-us/library/aa260621.aspx).

Published Friday, April 25, 2008 2:55 PM by sweisfeld
Filed under:

Comments

No Comments