SQL Server restore proc with database name parameter

1.7k views Asked by At

Looking for a stored procedure to restore from a .bak file, but would like to be able to enter the db name as a parameter, i.e. Exec sp_restore @dbname

  • The paths to the .bak will be identical for all the dbs i.e. all dbs are dev copies of the same production backup.
  • Each .mdf and .ldf will have the same name as the db itself i.e. dbname = @dbname, mdf = D:\data\@dbname.mdf, ldf =D:\data\@dbname.ldf
  • The paths to .mdf and .ldf will be the identical for each db, i.e. D:\data\@dbname.mdf
2

There are 2 answers

6
Guillaume CR On BEST ANSWER

Using Sql Server Management Studio, you can start performing a restore operation. Before completing the task, you should be able to see a "Script" button. This will create a script with all the parameters you have manually entered, including mdf and ldf location. You can then save this script and execute it at will. You can also modify the resulting script to make the database name an input variable. You can do anything really. It's SQL!

Script button

A sample script that restores a database and changes mdf and ldf file locations would look like this:

RESTORE DATABASE [example] FROM  DISK = N'E:\Backup\example.BAK' WITH  FILE = 1,  MOVE N'ExampleData' TO N'E:\dbfiles\example.mdf',  MOVE N'example_log' TO N'E:\dbfiles\example.ldf',  NOUNLOAD,  STATS = 10
GO

You can read more about the RESTORE statement

You can then insert the script in a stored procedure as such:

CREATE PROCEDURE RestoreDb
    -- Add the parameters for the stored procedure here
    @dbName nvarchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    RESTORE DATABASE @dbName FROM  DISK = N'C:\Data\MSSQL\Backup\lolwut.bak' WITH  FILE = 1,  NOUNLOAD,  STATS = 10

END
GO
0
Denis Rubashkin On

Procedure for restore DB from bak-file @DeviceName. It works with DBs, wich have two logical files. How to use:

EXEC RestoreDb @dbName='qqq', @DeviceName = 'D:\temp\1\R.bak'

Sorry for my English, I improve it.

CREATE PROCEDURE RestoreDb
    @dbName NVARCHAR(50),
    @DeviceName NVARCHAR(400)
AS
    SET NOCOUNT ON

    DECLARE @Cmd NVARCHAR(1000),
        @DataLogicalName NVARCHAR(200),
        @LogLogicalName NVARCHAR(200),
        @DatabasePath   NVARCHAR(200),
        @DataPath NVARCHAR(300),
        @LogPath  NVARCHAR(300)

    CREATE TABLE #Files
        (
        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)
        )

    SELECT @DatabasePath = 'D:\data\'

    SELECT @DataPath = @DatabasePath + @dbName + '.mdf',
        @LogPath = @DatabasePath + @dbName + '.ldf'

    SELECT @Cmd = 'RESTORE FILELISTONLY
    FROM DISK = ''' + @DeviceName + ''''

    INSERT #Files
    EXEC (@Cmd)

    IF NOT EXISTS(SELECT 1 FROM #Files) GOTO ERRORFILES

    IF (SELECT COUNT(*) FROM #Files) > 2 GOTO ERRORFILESCOUNT

    SELECT @DataLogicalName = LogicalName
    FROM #Files
    WHERE [Type] = 'D'

    SELECT @LogLogicalName = LogicalName
    FROM #Files
    WHERE [Type] = 'L'

    RESTORE DATABASE @DbName
    FROM DISK = @DeviceName
    WITH 
    MOVE @DataLogicalName TO @DataPath,
    MOVE @LogLogicalName  TO @LogPath

GOTO EXITSTAT

ERRORFILES:
    BEGIN
        RAISERROR( 'The list of files contained in the backup set is empty', 16, 1 )
        GOTO EXITSTAT
    END

ERRORFILESCOUNT:
    BEGIN
        RAISERROR( 'The count of files contained in the backup set is more than two', 16, 1 )
        GOTO EXITSTAT
    END

EXITSTAT: