How to restore database from network workgroup path?

139 Views Asked by At
USE MASTER
GO
ALTER DATABASE MyDB
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE

RECONFIGURE
GO

-- To enable xp_cmdshell
EXEC sp_configure 'xp_cmdshell', 1
GO
-- Update currently configured values for advanced options.
RECONFIGURE
GO

EXEC xp_cmdshell 'NET USE Z: \\Server\path passoword /USER:DOMAIN-NAME\UserName'

RESTORE DATABASE MyDB
FILE = N'logicalName'
FROM DISK = N'Z:\backupfilename'
WITH REPLACE,
FILE = 1, NOUNLOAD, STATS = 10,
MOVE N'logicalName'
TO N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\MyDB.mdf',
MOVE N'logicalName_log'
TO N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\MyDB_log.ldf'

EXEC xp_cmdshell 'net use Z: /delete'
GO

ALTER DATABASE MyDB SET MULTI_USER
GO

-- If there is no error in statement before database will be in multiuser mode. -- If error occurs please execute following command it will convert database in -- multi user.

0

There are 0 best solutions below