[英]SQL Server restore proc with database name parameter
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, ie Exec sp_restore @dbname
正在寻找要从.bak文件还原的存储过程,但希望能够输入数据库名称作为参数,即Exec sp_restore @dbname
dbname = @dbname
, mdf = D:\\data\\@dbname.mdf
, ldf =D:\\data\\@dbname.ldf
每个.mdf和.ldf将具有与数据库本身相同的名称,即dbname = @dbname
mdf = D:\\data\\@dbname.mdf
dbname = @dbname
, mdf = D:\\data\\@dbname.mdf
, ldf =D:\\data\\@dbname.ldf
D:\\data\\@dbname.mdf
每个数据库的.mdf和.ldf路径都相同,即D:\\data\\@dbname.mdf
Using Sql Server Management Studio, you can start performing a restore operation. 使用Sql Server Management Studio,您可以开始执行还原操作。 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. 这将创建一个脚本,其中包含您手动输入的所有参数,包括mdf和ldf位置。 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! 是SQL!
A sample script that restores a database and changes mdf and ldf file locations would look like this: 还原数据库并更改mdf和ldf文件位置的示例脚本如下所示:
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 您可以阅读有关RESTORE语句的更多信息
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
Procedure for restore DB from bak-file @DeviceName. 从bak文件@DeviceName还原数据库的过程。 It works with DBs, wich have two logical files. 它与DBs一起使用,具有两个逻辑文件。 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:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.