copy a database within SQL Server Express?

2019-01-16 12:03发布

I would like to make a copy of a database I have but keep it on the same server as a test database. However, everything I have found is to use the copy database wizard (I am using MS SQL Server Express).

The instructions always say: In SQL Server Management Studio, in Object Explorer, expand Databases, right-click a database, point to Tasks, and then click Copy Database.

I don't have the Copy Database option. I am running as an admin, so no clue why it is missing for me - is it something I have to install separately? I can't do the Detach/Attach since it is copying to the same server. I tried detaching, copying the MDF/LDF, renaming, attaching but as you can imagine that messed a ton up :) I am not great with SQL to do it all programatically. Is there a tool out there I could use?

12条回答
Root(大扎)
2楼-- · 2019-01-16 12:44

In SQL Server Express 2012 you can do following steps:

  1. Create a backup of the database you want to copy
  2. right-click "Databases" and select "Restore Files and Filegroups"
  3. Enter the name of the new database in the "To database" field.
  4. Select "From device" and then select the file that you backuped in the first step
  5. click "OK"

this will "clone" the Database with the correct table settings such as the "default value" and "auto increase" etc.

查看更多
家丑人穷心不美
3楼-- · 2019-01-16 12:47

I do not believe the Express version of the manager will have the copy database feature. Have you considered copying via the backup and restore method?

http://msdn.microsoft.com/en-us/library/ms190436.aspx

查看更多
祖国的老花朵
4楼-- · 2019-01-16 12:47

I found the problem! Click on Databases, restore, then do the following: After choosing from where to restore, and writing destination db name, go to files [annotation 1 on picture] and change the very right column files names to different than original [annotation 2 on picture] then it works :)

> SEE THE PICTURE HERE <

查看更多
贼婆χ
5楼-- · 2019-01-16 12:51

Take these steps to make a copy of the database in SQL Express

  1. Stop SQL
  2. Copy the mdf, ldf and any other file for the db to a NEW location (make sure you get the log file)
  3. Change the name of each copied file
  4. Start SQL
  5. Right-click Database in SSMQ and select attach
  6. Make sure you change the name in the column "Attach As"
  7. Update the file location in the lower pane of "Database Details" to the location of your copied files (especially that log file)

I was able to copy a database on my SQL Express system with this method

alt text

查看更多
倾城 Initia
6楼-- · 2019-01-16 12:53

Just be aware if you are using SQL Server Express 2012 of going to the option Files and make sure that the destination files (Restore As column) are different from the original files *.mdf and *.log

( I tried to put an image but need 10 reputation :p)

查看更多
欢心
7楼-- · 2019-01-16 12:53

The solution is definitely to create a backup and restore it, but ensure that you're restored copy is pointing to different .mdf and .ldf files.

Here's how to check that using SSMS 2014 and a SQL Server 12 installation: assuming you're creating and restoring backups on your local disk.

  • Create a backup of your existing database
    • Right click the database, and choose "back up..." under "tasks."
    • (If you leave the location as the default, you don't have to hunt for the back up in the next step when you restore.)
  • Restore your backup to a NEW database:
    • Right click on databases, choose "restore database"
    • Select "device"
    • Click the ellipsis button ("...") to open the "Selct Backup devices" dialog.
    • Choose "File" as the backup media type and click the "add" button
    • Select the backup you just made, click ok (twice)
    • Now, back in the "restore database" dialog, type a new name for your destination database
    • Click "files" under "select a page" and make sure that "restore as" is pointing to .mdf and .ldf file names that do not already exist
    • Click ok!
查看更多
登录 后发表回答