It is possible to do what you ask programmically. These are the steps:
- Test if your "source_test" database exists, and if so, delete it.
- Connect to your source database,
- Create a backup of the source database
- Restore the source database to "source_test" database while renaming the actual database files
- Execute the SQL Server utility stored procedure,
exec sp_msforeachtable 'truncate table ?'
This creates a schematically identical database of your "source" database named "source_test". All the triggers, constraints, stored procedures, SQL Server accounts, and every other bit of data that isn't the data table contents or sequence numbers is preserved. All the sequence numbers will be reset to default value when their tables are truncated.
We use these 5 steps to clone our own SQL Server databases for testing and other uses. This works on SQL Server 2000 forward.
To backup the database, you execute the SQL statement in the form of:
"Backup database " + kstrDbName + " to disk = '" + strFullyPathedBackupFile + "' With Init";
"With Init" is a key option that tells SQL Server to overwrite the file specified if it exists. More information can be found by looking up the SQL Server documentation for "Backup database".
On SQL Server 2005 and newer, you can use the SMO to restore the database, and specify the database's new name, as well as renaming the database files (the mdf and the log file). Example code of how to do this can be found in the Microsoft SQL Server SMO documentation on the Restore functionality.
On SQL Server 2000, there is no SMO support, so it must be done manually, through executed SQL statements. That takes the form statement similar to:
string strSQL = "Restore database " + strDatabaseName + "from disk = '" + strSourcePathFile + "' with replace, " +
" Move '" + strDatabaseName + "_Data' to '" + strDestinationPathFile + ".MDF', " +
" Move '" + strDatabaseName + "_Log' to '" + strDestinationPathFile + ".LOG' "
Then you use an Alter statement to rename the actual MDF and LOG files to match the new name. Yes, this means on SQL Server 2000, you cannot have the source database and the destination database. Sorry. It might be possible to do the rename of the MDF and LOG file at the same time as the restore, but if so, my team couldn't get it to work. We've moved on to new SQL Server databases, but I thought I'd pull the old code for completeness for this answer.
The alter statement is in the form of:
string AlterDbSQL = " Alter database " + strDatabaseName + " MODIFY FILE(NAME = " + Path.GetFileNameWithoutExtension( strDestinationPathFile ) + "_DATA, NEWNAME = " + strDatabaseName + "_Data) " +
" Alter database " + strDatabaseName + " MODIFY FILE(NAME = " + Path.GetFileNameWithoutExtension( strDestinationPathFile ) + "_LOG, NEWNAME = " + strDatabaseName + "_LOG) "
Some notes: if all you are interested in is creating a test database, then you don't need to perform step 2 (connect to source) and step 3 (create backup) every time. Just create a "template" backup of your current database you want to test against by creating a backup, restoring it, and then truncating the tables. Back that database up. Now you have backup of a data content purged version of your database. It will be a smaller backup, so it will be faster to copy and faster to restore. All you need to do is add in your test data and it is ready for testing.