How do I truncate all tables in a schema?

How do I truncate all tables in a schema?

A solution that can TRUNCATE all tables

  1. Create a table variable to store the constraint drop and creation scripts for the database.
  2. Load the data for all tables in the database.
  3. Execute a cursor to drop all constraints.
  4. Truncate all tables.
  5. Recreate all the constraints.

How do I truncate a schema?

To truncate a table or cluster, the table or cluster must be in your schema or you must have DROP ANY TABLE system privilege. Specify the schema and name of the table to be truncated. This table cannot be part of a cluster. If you omit schema , then Oracle Database assumes the table is in your own cluster.

Can You truncate a table with a FOREIGN KEY constraint?

Using MSSQL2005, can I truncate a table with a foreign key constraint if I first truncate the child table (the table with the primary key of the FK relationship)? Remove the FK, truncate the table, and recreate the FK.

Is there a way to truncate a table?

— TRUNCATE TABLES — SzP: commented out as the tables to be truncated might also contain tables that has foreign keys — to resolve this the stored procedure should be called recursively, but I dont have the time to do it…

Why is table name cannot be truncated in SQL Server?

Cannot truncate table ‘TableName’ because it is being referenced by a FOREIGN KEY constraint. Correct; you cannot truncate a table which has an FK constraint on it. Recreate the constraints.

Is there a way to remove the FK constraint?

You can of course force your way by removing the FK constraint. But then you will not be able to re-enable it since employees now exist without the parent department. There are even options (CASCADE) that tells the database to automatically delete the child rows: delete the department and fire all employees in that department in one step.