USE master;
GO

DECLARE @SrcDB NVARCHAR(128) = 'ProdDUTASJobSchedule';  -- original database
DECLARE @NewDB NVARCHAR(128) = 'ProdDUTASJobSchedule';  -- database name to restore as
DECLARE @BackupPath NVARCHAR(260) = '\\DOES-RAINVM-PRD\E$\Neeraj\SqlAgent\Backup\' + @SrcDB + '.bak';
DECLARE @SQL NVARCHAR(MAX);

-- Optional: check logical file names inside backup
-- RESTORE FILELISTONLY FROM DISK = @BackupPath;

-- Dynamic SQL for restore
SET @SQL = '
-- Optional: disconnect active connections
ALTER DATABASE [' + @NewDB + '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

RESTORE DATABASE [' + @NewDB + ']
FROM DISK = N''' + @BackupPath + '''
WITH MOVE N''' + @SrcDB + ''' TO N''\\DOES-RAINVM-PRD\E$\SQLData\' + @NewDB + '.mdf'',
     MOVE N''' + @SrcDB + '_log'' TO N''\\DOES-RAINVM-PRD\E$\SQLLogs\' + @NewDB + '_log.ldf'',
     REPLACE, STATS = 10;

-- Set database back to multi-user
ALTER DATABASE [' + @NewDB + '] SET MULTI_USER;
';

-- Execute restore
EXEC(@SQL);

PRINT 'Database restored successfully as ' + @NewDB;
GO
