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"