I*'ve got a refresh script I came across and was adjusting to suit our environment. There seems to be something missing as I get the following error while parsing the sql script
"Msg 137, Level 15, State 2, Line 3
Must declare the scalar variable "@FileName".
Msg 319, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Below is the actual script that finds the latest backup, and creates the restore based on the @Filename
DECLARE @tableDir TABLE
( RecordID INT IDENTITY
, FileDateTime DATETIME NULL
, FilePathName VARCHAR(MAX) NULL
)
INSERT INTO @tableDir (FilePathName)
EXEC master..xp_cmdshell 'DIR
\\server2\k$\SQL_Backup\PA880PRD\PA880PRD_Full_*.bak'
DELETE
FROM @tableDir
WHERE (
FilePathName IS NULL
OR FilePathName LIKE '%Volume %'
OR FilePathName LIKE '%Directory %'
OR FilePathName LIKE '% File(s) %'
OR FilePathName LIKE '% Dir(s) %'
)
UPDATE @tableDir
SET FileDateTime = CAST(LEFT(FilePathName, 20) AS DATETIME)
UPDATE @tableDir
SET FilePathName = LTRIM(RTRIM(RIGHT(FilePathName, LEN(FilePathName) - PATINDEX('%PA880PRD_Full_%', FilePathName) + 1)))
SELECT TOP 1 @File = N'\\server2\k$\SQL_Backup\PA880PRD\' + FilePathName
FROM @tableDir
ORDER BY FilePathName DESC
DECLARE @FileName VARCHAR(1000)
SET @FileName = CAST(@File AS VARCHAR(1000))
ALTER DATABASE PA880RPT SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE PA880RPT SET MULTI_USER;
GO
RESTORE DATABASE [PA880RPT]
FROM DISK = @FileName
WITH FILE = 1
, MOVE N'EP88SYS_Data' TO N'H:\SQL_Data\PA880RPT_Data.mdf'
, MOVE N'EP88SYS_Log' TO N'I:\SQL_Log\PA880RPT_Log.ldf'
, NOUNLOAD
, REPLACE
, STATS = 10
GO
Appreciate replies.