How to restore to a different database in sql server?

后端 未结 10 1574
忘掉有多难
忘掉有多难 2020-11-29 14:39

I have a backup of Database1 from a week ago. The backup is done weekly in the scheduler and I get a .bak file. Now I want to fiddle with some

相关标签:
10条回答
  • 2020-11-29 15:02

    If no database exists I use the following code:

    ALTER PROCEDURE [dbo].[RestoreBackupToNewDB]    
             @pathToBackup  varchar(500),--where to take backup from
             @pathToRestoreFolder  varchar(500), -- where to put the restored db files 
             @newDBName varchar(100)
        AS
        BEGIN
    
                SET NOCOUNT ON
                DECLARE @fileListTable TABLE (
                [LogicalName]           NVARCHAR(128),
                [PhysicalName]          NVARCHAR(260),
                [Type]                  CHAR(1),
                [FileGroupName]         NVARCHAR(128),
                [Size]                  NUMERIC(20,0),
                [MaxSize]               NUMERIC(20,0),
                [FileID]                BIGINT,
                [CreateLSN]             NUMERIC(25,0),
                [DropLSN]               NUMERIC(25,0),
                [UniqueID]              UNIQUEIDENTIFIER,
                [ReadOnlyLSN]           NUMERIC(25,0),
                [ReadWriteLSN]          NUMERIC(25,0),
                [BackupSizeInBytes]     BIGINT,
                [SourceBlockSize]       INT,
                [FileGroupID]           INT,
                [LogGroupGUID]          UNIQUEIDENTIFIER,
                [DifferentialBaseLSN]   NUMERIC(25,0),
                [DifferentialBaseGUID]  UNIQUEIDENTIFIER,
                [IsReadOnly]            BIT,
                [IsPresent]             BIT,
                [TDEThumbprint]         VARBINARY(32) -- remove this column if using SQL 2005
                )
                INSERT INTO @fileListTable EXEC('RESTORE FILELISTONLY FROM DISK ='''+ @pathToBackup+'''')
                DECLARE @restoreDatabaseFilePath NVARCHAR(500)
                DECLARE @restoreLogFilePath NVARCHAR(500)
                DECLARE @databaseLogicName NVARCHAR(500)
                DECLARE @logLogicName NVARCHAR(500)
                DECLARE @pathSalt uniqueidentifier = NEWID()
    
                SET @databaseLogicName = (SELECT LogicalName FROM @fileListTable WHERE [Type]='D') 
                SET @logLogicName = (SELECT LogicalName FROM @fileListTable WHERE [Type]='L')           
                SET @restoreDatabaseFilePath= @pathToRestoreFolder + @databaseLogicName + convert(nvarchar(50), @pathSalt) + '.mdf'
                SET @restoreLogFilePath= @pathToRestoreFolder + @logLogicName + convert(nvarchar(50), @pathSalt) + '.ldf'
    
                RESTORE DATABASE @newDBName FROM DISK=@pathToBackup     
                WITH 
                   MOVE @databaseLogicName TO @restoreDatabaseFilePath,
                   MOVE @logLogicName TO @restoreLogFilePath
    
                SET NOCOUNT OFF
        END
    
    0 讨论(0)
  • 2020-11-29 15:04

    Actually, there is no need to restore the database in native SQL Server terms, since you "want to fiddle with some data" and "browse through the data of that .bak file"

    You can use ApexSQL Restore – a SQL Server tool that attaches both native and natively compressed SQL database backups and transaction log backups as live databases, accessible via SQL Server Management Studio, Visual Studio or any other third-party tool. It allows attaching single or multiple full, differential and transaction log backups

    Moreover, I think that you can do the job while the tool is in fully functional trial mode (14 days)

    Disclaimer: I work as a Product Support Engineer at ApexSQL

    0 讨论(0)
  • 2020-11-29 15:08
    • I have the same error as this topic when I restore a new database using an old database. (using .bak gives the same error)

    • I Changed the name of old database by name of new database (same this picture). It worked.

    0 讨论(0)
  • 2020-11-29 15:08
    1. make a copy from your database with "copy database" option with different name
    2. backup new copied database
    3. restore it!
    0 讨论(0)
  • 2020-11-29 15:11

    You can create a new db then use the "Restore Wizard" enabling the Overwrite option or;

    View the content;

    RESTORE FILELISTONLY FROM DISK='c:\your.bak'
    

    note the logical names of the .mdf & .ldf from the results, then;

    RESTORE DATABASE MyTempCopy FROM DISK='c:\your.bak'
    WITH 
       MOVE 'LogicalNameForTheMDF' TO 'c:\MyTempCopy.mdf',
       MOVE 'LogicalNameForTheLDF' TO 'c:\MyTempCopy_log.ldf'
    

    To create the database MyTempCopy with the contents of your.bak.

    Example (restores a backup of a db called 'creditline' to 'MyTempCopy';

    RESTORE FILELISTONLY FROM DISK='e:\mssql\backup\creditline.bak'
    
    >LogicalName
    >--------------
    >CreditLine
    >CreditLine_log
    
    RESTORE DATABASE MyTempCopy FROM DISK='e:\mssql\backup\creditline.bak'
    WITH 
       MOVE 'CreditLine' TO 'e:\mssql\MyTempCopy.mdf',
       MOVE 'CreditLine_log' TO 'e:\mssql\MyTempCopy_log.ldf'
    
    >RESTORE DATABASE successfully processed 186 pages in 0.010 seconds (144.970 MB/sec).
    
    0 讨论(0)
  • 2020-11-29 15:12

    It is actually a bit simpler than restoring to the same server. Basically, you just walk through the "Restore Database" options. Here is a tutorial for you:

    http://www.techrepublic.com/blog/window-on-windows/how-do-i-restore-a-sql-server-database-to-a-new-server/454

    Especially since this is a non-production restore, you can feel comfortable just trying it out without worrying about the details too much. Just put your SQL files where you want them on your new server and give it whatever name you want and you are good to go.

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