简体   繁体   中英

Copy Sql Server 2008 Databse schema only to Another Sql server 2008 Database on same machine

I have a Sql server 2008 Database

Is there any way to create new database and copy the schema of existing database objects to newly created database using Sql query or Stored Procedure

First you'll need to make a full backup of the database you want to copy:

BACKUP DATABASE Database1
 TO DISK = 'X:\FullPath\AdvWorksData.bak'
   WITH FORMAT;

where X:\\FullPath is the full path to a location you can backup to on disk.

Next you'll need to create the new database (if you haven't already):

CREATE DATABASE Database2;

And then finally you'll need to restore over the top of that database:

RESTORE FILELISTONLY 
   FROM Database1;

RESTORE DATABASE Database2
   FROM Database1
   WITH MOVE 'Database1_Data' TO 'X:\FullPath\Database2.mdf',
   MOVE 'Database1_Log' TO 'X:\FullPath\Database2.ldf';
GO

where X:\\FullPath is the full path to where you're second databases files exist.

Some Caveats

  1. MOVE 'Database1_Data' and 'Database1_Log' are the default logical file names. Those can differ. You can find those logical names under the properties of the database.
  2. 'X:\\FullPath\\Database2.mdf' and 'X:\\FullPath\\Database2.ldf' may not both exist in the same location and can differ from the default locations. You can find those locations under the properties of the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM