Posts

Showing posts with the label Scripts

AUTOMATED RESTORE TLOG SCRIPT

   AUTOMATED RESTORE TLOG SCRIPT  "In the event of database corruption or data loss, if multiple log or differential files need to be restored, this script is capable of generating the necessary restore script for all log files to be restored onto the database." USE Master;  GO   SET NOCOUNT ON  -- 1 - Variable declaration  DECLARE @dbName sysname  DECLARE @backupPath NVARCHAR(500)  DECLARE @cmd NVARCHAR(500)  DECLARE @fileList TABLE (backupFile NVARCHAR(255))  DECLARE @lastFullBackup NVARCHAR(500)  DECLARE @lastDiffBackup NVARCHAR(500)  DECLARE @backupFile NVARCHAR(500)  -- 2 - Initialize variables  SET @dbName = 'Customer'  SET @backupPath = 'D:\SQLBackups\'  -- 3 - get list of files  SET @cmd = 'DIR /b "' + @backupPath + '"' INSERT INTO @fileList(backupFile)  EXEC master.sys.xp_cmdshell @cmd  -- 4 - Find latest full backup  SELECT @lastFullBackup = MAX(backupFile)   FROM @fileList   WHERE backupFile LIKE '%.BAK'      AND ba