MS SQL 2008 - Create a copy of the database without the data MS SQL 2008 - Create a copy of the database without the data sql-server sql-server

MS SQL 2008 - Create a copy of the database without the data


Right click the Database and select Tasks -> Generate Scripts.

You can you then select all the objects you require or only certain objects.

There are some Script Options that you should look at:

  • Script USE DATABASE - You should set this to false if you are creating a new database with a different name.
  • Indexes & Triggers are not scripted as default
  • Logins & Object Level Permissions are not scripted as default
  • Generate Script for Dependent objects - this is set to false as default but you may want to change it to true to ensure that all objects are scripted.

You can either create these to a new Query Window or save them to SQL Files.


Summary

This script was created to make it easier to practice database migration of a major website between very-different database schema. This script will perform the following tasks:

  1. Backup the latest good version of your schema, with data. (Example: Dev Database)
  2. Drop the target database, if it exists
  3. Restore the backup to the target testing database
  4. Delete all data in the target database, despite constraints and foreign keys

Usage Instructions

Part 1

You will probably want to obtain the code for this from within SSMS using the backup dialog. Click on the circled icon to obtain the script for your specific scenario.

  • If you do this, remember to set "overwrite" in the backup options before generating script -- we don't want to append to a backup set.

enter image description here

Part 2

Don't forget to include the USE MASTER command before dropping the destination database. If you run this script for the second time in a single SSMS session, you will get a fatal error if you don't include this command. We also use DROP DATABASE here to not only remove the stale TargetDB, but also delete its files (Note, DB must be "active" for this to work)

Part 3

You may want to use the SSMS dialog to generate this initial restore script for you, similar to how you did it for Part 1 for Backup.

Part 4

Don't use Truncate here -- it won't work if you have foreign keys

Source Code (run in SSMS)

------------------------------------------------------------------------------------------ CREATE AN EMPTY COPY OF DATABASE----------------------------------------------------------------------------------------/* PART 1: Backup the good database */BACKUP DATABASE [OriginalDB]         TO  DISK = N'd:\backup.bak' WITH NOFORMAT, INIT,              NAME = N'OriginalDB-Full Database Backup', SKIP,         NOREWIND, NOUNLOAD,  STATS = 33GO/* PART 2: If your destination database already exists, drop it */USE master -- Make sure to include this -- it allows you to reuse script in same SSMS sessionDROP DATABASE [migration]/* PART 3: Restore the backup to the new location */RESTORE DATABASE [TargetDB]         FROM  DISK = N'D:\backup.bak' WITH  FILE = 1,          MOVE N'OriginalDB' TO N'D:\sql data\TargetDB.mdf',          MOVE N'OriginalDB' TO N'C:\SQL Data\TargetDB_1.ldf',          NOUNLOAD,  STATS = 33GO/* PART 4: Delete all tables' data in the migration testing target */PRINT N'Clearing [TargetDB]'USE [TargetDB]EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"       -- disable all constraintsEXEC sp_MSForEachTable "DELETE FROM ?"                  -- delete data in all tablesexec sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"  -- enable all constraints------------------------------------------------------------------------------------------ BLANK DATABASE COPY CREATED, READY FOR TESTING----------------------------------------------------------------------------------------


There is a nice script wizard that can help you. You can try it with Right Click on the database from the object explorer -> Tasks -> Generate Scripts. You can choose which objects from the database you want to export.