November 27, 2006 at 7:38 am
Hi there.
Is there a way in which I can delete all the data in all the tables in a given database. I just want to delete the data in these tables, not the actual tables or columns.
how would this be done via tsql and/or enterprise manager.
Many thanks
November 27, 2006 at 7:44 am
RUN only in developpmenet area :
EXEC sp_MSForEachTable 'DELETE FROM ?'
November 27, 2006 at 8:02 am
Or use Truncate instead of delete.
November 27, 2006 at 8:13 am
I'm assuming that there is at least 1 foreign key in the whole DB. So truncate was out of the question .
November 27, 2006 at 9:00 am
EXEC sp_MSForEachTable 'DELETE FROM ?'
what stored procedure is this?
November 27, 2006 at 9:04 am
System procedure in the master database. It looks through all tables in the current database and executes the statement passed in paramter. The question mark is used as a placeholder for the fully qualified table name.
You can run this to see how it works :
EXEC sp_MSForEachTable 'Select ''?'''
November 27, 2006 at 1:21 pm
If there are FK's the order of tables to delete data from is really important.
_____________
Code for TallyGenerator
November 27, 2006 at 1:35 pm
Ya that was the lazy solution. Ultimatelly you could run it untill no row are affected, meaning the db should be empty (excluding circular reference of course).
But I don't have time to write a script to find the delete order. Anyone got one handy?
November 27, 2006 at 1:51 pm
Right-click on the database in EM. Choose All Tasks - Generate SQL Script.
Include all objects. Run. Save it. Run to create new empty database.
Runs faster than delete from a single table.
_____________
Code for TallyGenerator
November 27, 2006 at 2:12 pm
Good point... that's why I want to move to a bigger company ( meaning 100 GB db ). You don't learn that stuff on a 300mb db .
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply