I have the following script to restore a database. On the line that says:
FROM DISK = @backup_location --N'C:\Temp\TempDB.bak'
If I use the @backup_location variable as above, it fails with the error:
Msg 3201, Level 16, State 2, Line 24 Cannot open backup device 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\Backup\C'. Operating system error 2(The system cannot find the file specified.). Msg 3013, Level 16, State 1, Line 24 RESTORE DATABASE is terminating abnormally.
If I hard code the location using:
FROM DISK = N'C:\Temp\TempDB.bak'
It works fine.
How can I use a variable to specify the database path?
USE [master]
Go
-- DECLARE VARIABLES:
DECLARE @dbname SYSNAME
DECLARE @backup_location VARCHAR
SET @dbname = 'TempDB';
SET @backup_location = N'C:\Temp\TempDB.bak';
-- BEGIN: KILL ACTIVE CONNECTIONS
Declare @spid int
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname)
While @spid Is Not Null
Begin
Execute ('Kill ' + @spid)
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname) and spid > @spid
End
-- END: KILL ACTIVE CONNECTIONS
-- RESTORE DB:
RESTORE DATABASE @dbname
FROM DISK = @backup_location --N'C:\Temp\TempDB.bak'
WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
Go
Thanks for your time.