Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete all Records but Keep Table Structure 2

Status
Not open for further replies.

jmiller79

Programmer
Jul 13, 2004
48
US
How do I delete all the Records in the table but keep the table structure the same. Any help would be great,

Thanks
Joe
 
Code:
truncate table tablename

where tablename is the name of the table you want to delete the records from.

If you only want delete certain records, you can do this:

Code:
Delete tablename where ...


 
Note that using truncate table is dangerous, this is not a logged operation. Only use this if you have good recent backups or can readily reload the data from another source. If you want the deltion logged, then use the delte command without a where clause. And make darn sure you are in the right database on the right server and using the right table. If you are thinking you are clearing out the Customer table on your development server and you accidentally do it on the production server, then that is bad news even if you have a recent backup. People have lost their jobs for careless use of truncate table.

Questions about posting. See faq183-874
 
Very true SQLSister, but there is one other thing to mention about the difference between DELETE and TRUNCATE - if you use and identity column, TRUNCATE resets the identity to the seed (usually 1) - this is actually my determining factor in which one to use.

(side note - I've never used TRUNCATE TABLE on a production database... Only development systems)
 
Keep in mind that TRUNCATE TABLE is much more efficient in terms of performance. It flips a bit switch in the space map header and does NOT perform physical deletes row by row. (DELETE FROM performs row by row deletes and is a performance hog for very large tables)
 
and (just to add to btturner..)
Be prepared for logging!
As Delete is a logged action 2 gig of data being truncated can take up more than the 2gig by way of logfiles.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top