Friday, March 30, 2012

How to truncate complete database?

Hello,

I want to truncate all tables present in the particular database, Is there any simple way to do it? or do I have to do it on individula basis (table by table)?

regards

When you say truncate, do you just want to remove all records from all tables? The easiest way would be to script out the tables with drop and create statements. To actually truncate the tables, you could write a cursor to loop through the table names and to run dynamic sql to truncate each table.

I hope this helps.

|||

Zadoras wrote:

Hello,

I want to truncate all tables present in the particular database, Is there any simple way to do it? or do I have to do it on individula basis (table by table)?

regards

create a script to drop all your FK constraints

create a script to create all your FK constraints

To do this, right click on the database and "Generate Scripts.."

Then

1 - run your script to drop all Fks

2 - use the sp_MSForEachTable stored procedure to truncate all tables

EXEC sp_MSForEachTable 'Truncate Table ?'

3 - run your script to add all the FKs back|||thanx very much for your support

No comments:

Post a Comment