Home Forums SQL Server 2005 Backups Loading database with the latest .bak file RE: Loading database with the latest .bak file

  • I will give you an outline of how to achieve this

    DECLARE @InsertedRecords INT

    CREATE TABLE #GetFileList

    (

    SrNo Int IDENTITY (1,1),

    FileListOutPut Varchar(1000)

    )

    INSERT INTO #GetFileList (FileListOutPut)

    EXEC master.dbo.xp_cmdshell 'DIR \\ \*.bak /OD /-C /B'

    -- /OD is the sort order by date/time (oldest first)

    -- /-C to disable display of seperator in the size value

    -- /B no heading information or summary

    DELETE FROM #GetFileList

    WHERE FileListOutPut IS NULL

    SELECT @InsertedRecords= COUNT(*) FROM #GetFileList

    SELECT FileListOutPut AS BackupFile_Name FROM #GetFileList

    WHERE SrNo = @InsertedRecords

    DROP TABLE #GetFileList

    DROP TABLE #GetFileList