0

Question

In SQL Server Manager 2008/2012 it is possible to create a a complete clone of an existing database.
Is there a way to generate this kind of script programmatically?

[Database] -> Right Click -> Tasks -> Generate Script

What I've tried

I already tried to recreate my database programmatically and delete all rows from all tables. But it seems not to be possible to disable all foreign key constraints. I've found some scripts on SO that can drop and recreate constraints but they won't work with clustered constraints.

-- disable trigger / fk constraints
EXEC sp_MSForEachTable "ALTER TABLE ? DISABLE TRIGGER ALL"
EXEC sp_MSForEachTable "ALTER TABLE ? NOCHECK CONSTRAINT ALL"

-- truncate all tables
EXEC sp_MSForEachTable "DELETE FROM ?"

-- enable trigger / constraints
EXEC sp_MSForEachTable "ALTER TABLE ? ENABLE TRIGGER ALL"
EXEC sp_MSForEachTable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL"
2

1 Answer 1

0

Not sure what you mean by programatically, but the Generate scripts you mentioned above above should do the job. I will create the script to recreate your database and its schema without any data in it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.