use Master
-- This code restores all logs that have been copied to C:\inout on the HealthOne server to the Transaction Central Database
-- and marks them as Processed (sets 0 to 1 - or True) in the TC Database, TransLog_Status table.
Declare @Logname char(50),
@Errnum int,
@Countloop int,
@spid as varchar(10),
@CMD as varchar(1000);
-- The following Cursor kills all connections on the Transaction Central database.
declare cur CURSOR for select spid from master.dbo.sysprocesses where dbid =
(select dbid from sysdatabases where name = 'TransactionCentral')
open cur
fetch next from cur into @spid
while @@FETCH_STATUS = 0
BEGIN
set @CMD = 'kill ' + @spid
exec (@CMD)
fetch next from cur into @spid
END
close cur
deallocate cur
-- The following one line command changes the database to Single User so the restore can complete properly.
Exec sp_dboption @dbname = 'TransactionCentral', @optname = 'Single user', @optvalue = 'True';
Set @Errnum = 0;
Set @countloop = (Select count(logname) from tc.dbo.translog_status where processed = 0);
While @countloop <> 0
Begin
Set @logname = (Select min(logname) from tc.dbo.translog_status where processed = 0);
Restore Log TransactionCentral
from disk=@logname
with Standby = 'd:\inout\undos\undo.dat';
Set @errnum = @@Error;
If @errnum = 0
Begin Update tc.dbo.Translog_status
Set processed = 1
where Logname = @logname
End
Else
Break;
Set @countloop = @countloop - 1;
END;
-- The following one line command sets the database back to Multi-User mode so people can reconnect and do their work.
Exec sp_dboption @dbname = 'TransactionCentral', @optname = 'single user', @optvalue = 'False';
-- This code records an error number in the TC Database, TransLog_Status table for the Transaction Log backup where the
-- restore failed. This error code can be used in trouble shooting.
If @errnum <> 0
Begin
Use MSDB;
Exec sp_update_job @job_name = Restore_TC, @enabled = 0 ;
Use TC;
Update tc.dbo.Translog_status
Set Errnum = @errnum
Where Logname = @logname
END;