In MS SQL Server 2008 R2, how do I create a new database based on the schema of the old one, but without copying any of the data along with it? I am using SQL Server management
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.
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:
You can either create these to a new Query Window or save them to SQL Files.
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:
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.
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)
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.
Don't use Truncate here -- it won't work if you have foreign keys
----------------------------------------------------------------------------------------
-- 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 = 33
GO
/* 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 session
DROP 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 = 33
GO
/* 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 constraints
EXEC sp_MSForEachTable "DELETE FROM ?" -- delete data in all tables
exec sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all" -- enable all constraints
----------------------------------------------------------------------------------------
-- BLANK DATABASE COPY CREATED, READY FOR TESTING
----------------------------------------------------------------------------------------
In SQL Server Management Studio, you can menu-click on an object and select...
"Script [Object] As" ... "CREATE to"... "New Query Window"
You would need to create scripts for each object you want to create.
If you want to do the whole lot, menu-click on the database, and select
"Tasks" ... "Generate Scripts" and follow the wizard, as per this article: