How do I copy SQL Server 2012 database to localdb instance?

后端 未结 9 1884
你的背包
你的背包 2021-02-04 00:49

I\'m looking to copy a SQL Server 2012 Standard database to my localdb instance. I\'ve tried the wizard which complains that localdb isn\'t a SQL Server 2005 or later expres

相关标签:
9条回答
  • 2021-02-04 01:23

    Try these scripts (example with adventureworks2012 that I personally tested):

    RESTORE FILELISTONLY
    FROM DISK = 'c:\temp\adv2012.bak'
    

    This will bring up the filenames as:

    AdventureWorks2012      C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL2012RTM\MSSQL\DATA\AdventureWorks2012.mdf
    AdventureWorks2012_log  C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL2012RTM\MSSQL\DATA\AdventureWorks2012_log.ldf
    

    Use these filenames to cinstruct your final script as this:

    RESTORE DATABASE AdventureWorks2012
    FROM DISK = 'C:\temp\adv2012.bak'
    
    WITH MOVE 'AdventureWorks2012' TO 'C:\cnom_WS\Local-Databases\AdventureWorks\AdventureWorks2012.mdf',
    MOVE 'AdventureWorks2012_log' TO 'C:\cnom_WS\Local-Databases\AdventureWorks\AdventureWorks2012_log.ldf',
    REPLACE;
    

    BTW I run these through Visual Studio (SQL Server Object explorer), but I strongly suspect this could be run on SSMS easily ;-)

    0 讨论(0)
  • 2021-02-04 01:24

    Try scripting your database as schema and data and then running the script locally.

    0 讨论(0)
  • 2021-02-04 01:25

    I had the same issue, and after doing a little online research I came across an ingenious way to get it to work (albeit quite hacky). Basically, you:

    1. Create a SqlLocalDb instance (SqlLocalDb c tmp -s).
    2. Restore the database as you did above (e.g., SqlCmd -E -S <localdb connection string> -Q "RESTORE DATABASE ...").
    3. Stop the SqlLocalDb instance (SqlLocalDb p tmp).
    4. Delete the SqlLocalDb instance (SqlLocalDb d tmp).
    5. Create a new SqlLocalDb instance (SqlLocalDb c persistent -s).
    6. Create the database in the new instance by attaching it (SqlCmd -E -S <persistent connection string> -Q "Create Database <dbname> On (Filename = '<Mdf file location'), (Filename = '<Ldf Filename'>) For Attach".

    And hopefully it should work. See here for original idea.

    Edit: Added Jason Brady's correction of the create command.

    0 讨论(0)
提交回复
热议问题