USE master
go
IF OBJECT_ID('dbo.sp_restore_db_logs') IS NOT NULL And
OBJECTPROPERTY(OBJECT_ID('dbo.sp_restore_db_logs'), 'IsProcedure') = 1
BEGIN
DROP PROCEDURE dbo.sp_restore_db_logs
IF OBJECT_ID('dbo.sp_restore_db_logs') IS NOT NULL And
OBJECTPROPERTY(OBJECT_ID('dbo.sp_restore_db_logs'), 'IsProcedure') = 1
PRINT '<<< FAILED DROPPING PROCEDURE dbo.sp_restore_db_logs >>>'
ELSE
PRINT '<<< DROPPED PROCEDURE dbo.sp_restore_db_logs >>>'
END
go
CREATE PROCEDURE sp_restore_db_logs (
@SourceDir nvarchar( 512 ) = NULL ,
@DBBackup nvarchar( 512 ) = NULL ,
@SourceDB nvarchar( 128 ) = NULL ,
@DestDB nvarchar( 128 ) = NULL ,
@StartAt datetime = NULL ,
@StopAt datetime = NULL ,
@RestoreMode varchar( 10 ) = 'NORECOVERY' ,
@MoveDataTo varchar( 255 ) = NULL ,
@MoveLogTo varchar( 255 ) = NULL ,
@RecoverAtEnd bit = 0 ,
@Debug bit = 0 ,
@JustCheck bit = 0
)
WITH ENCRYPTION
AS
/*********************************************************************************/
/* Created By : Umachandar Jayachandran (UC) */
/* Created On : 06 September 2000 */
/* Description: This stored procedure can be used to load a database backup and */
/* several log backups using a programmable mechanism. This SP also */
/* accepts several parameters that can be used to control the log */
/* loads, recovery mode & so on. This also serves to demonstrate */
/* several powerful T-SQL programming techniques. */
/*********************************************************************************/
/* Resources : https://umachandar.com/resources.htm */
/*********************************************************************************/
CREATE TABLE #Files(
[FileName] varchar( 255 ) NULL ,
FileTimeStamp AS ( CAST( STUFF( STUFF( SUBSTRING( [FileName] ,
PATINDEX('%_' + REPLICATE( '[0-9]' , 12 ) + '%' , [FileName] ) + 1 ,
COALESCE( NULLIF( PATINDEX( '%.TRN', [FileName] ) , 0 ) ,
NULLIF( PATINDEX( '%.BAK', [FileName] ) , 0 ) ) -
PATINDEX('%_' + REPLICATE( '[0-9]' + '%' , 12 ),
[FileName] ) - 1 ) ,
9 , 0 , SPACE( 1 ) ) ,
12 , 0 , ':' ) AS datetime ) )
);
CREATE TABLE #DBFiles (
LogicalName nvarchar( 128 ) ,
PhysicalName nvarchar( 512 ) ,
Type char( 1 ) ,
FileGroupName nvarchar( 128 ) ,
[Size] varchar( 30 ) ,
[MaxSize] varchar( 30 ) ,
RevPhysicalName AS ( REVERSE( PhysicalName ) )
);
DECLARE @CmdStr varchar( 8000 ) , @LogExt varchar( 10 ) , @DBExt varchar( 10 ) ,
@crlf char( 2 ) , @headerline varchar( 255 ) , @DBBackupDir nvarchar( 512 ) ,
@FileName varchar( 255 ) , @LoadFile varchar( 255 ) , @StandbyFile varchar( 255 ) ,
@Source varchar( 10 ) , @IsTapeBackup bit , @DoDBRestore bit , @MoveDBFiles bit;
SELECT @DBExt = '.BAK' , @LogExt = '.TRN' , @crlf = CHAR( 13 ) + CHAR( 10 ) ,
@headerline = @crlf + REPLICATE( '-' , 80 ) + @crlf ,
@IsTapeBackup = CASE WHEN @DBBackup LIKE '\\.\Tape%' THEN 1 ELSE 0 END ,
@Source = CASE @IsTapeBackup WHEN 1 THEN 'TAPE' WHEN 0 THEN 'DISK' END ,
@DoDBRestore = CASE WHEN @DBBackup IS NULL THEN 0 ELSE 1 END ,
@MoveDBFiles = CASE WHEN COALESCE( @MoveDataTo , @MoveLogTo ) IS NULL THEN 0 ELSE 1 END ,
@SourceDir = @SourceDir + CASE RIGHT( @SourceDir , 1 ) WHEN '\' THEN '' ELSE '\' END ,
@MoveDataTo = @MoveDataTo + CASE RIGHT( @MoveDataTo , 1 ) WHEN '\' THEN '' ELSE '\' END ,
@MoveLogTo = @MoveLogTo + CASE RIGHT( @MoveLogTo , 1 ) WHEN '\' THEN '' ELSE '\' END ,
@StopAt = COALESCE( @StopAt , '9999-12-31 00:00' );
IF COALESCE( UPPER( @RestoreMode ) , 'NORECOVERY' ) NOT IN ( 'NORECOVERY' , 'STANDBY' )
BEGIN
RAISERROR( 'Invalid restore option was specified!', -1 , 12 );
RETURN( -1 );
END;
IF COALESCE( NULLIF( @SourceDir , '' ) , NULLIF( @SourceDB , '' ) ,
NULLIF( @DestDB , '' ) ) IS NULL
BEGIN
PRINT 'Usage: sp_restore_db_logs
This page was last updated on May 01, 2006 04:28 PM.