472,342 Members | 1,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Restore DB procedure - date parameter

I am using this procedure from net for getting the restore script. It
lists the latest full backup file name and logs after that point. Is
there any way to modify this script to take either date or time as
parameter and give the files for restore? I need this if I need to
restore the data up to last week/upto some point of time.
CREATE procedure usp_what_files_to_restore
as
--
--
--
-- Description:
-- This stored procedure generates a list of files that need to be
restored
-- should the server need to be rebuild. The list only contains a
list of database
-- backup files.
--
-- Declare variables used in SP
declare @cmd nvarchar (1000)
declare @cmd1 nvarchar (1000)
declare @db nvarchar(128)
declare @filename nvarchar(128)
declare @cnt int
declare @num_processed int
declare @name nvarchar(128)
declare @physical_device_name nvarchar(128)
declare @backup_start_date datetime
declare @type char(1)
-- Section A --------------------------------------------
-- Turn off the row number message
set nocount on
-- Define cursor to hold all the different databases for the restore
script will be built
declare db cursor for
select name from master..sysdatabases
where name not in ('tempdb')
-- Create a global temporary table that will hold the name of the
backup, the database name,
-- and the type of database backup.
create table ##backupnames (
name nvarchar(100),
database_name nvarchar(100),
type char(1) )
-- Open cursor containing list of database names.
open db
fetch next from db into @db
-- Process until no more databases are left
WHILE @@FETCH_STATUS = 0
BEGIN
-- Subsection 1A ----------------------------------------------
-- initialize the physical device name
set @physical_device_name = ""
-- get the name of the last full database backup
select @physical_device_name = physical_device_name ,
@backup_start_date = backup_start_date
from msdb..backupset a join msdb..backupmediaset b on a.media_set_id
= b.media_set_id
join msdb..backupmediafamily c on a.media_set_id =
c.media_set_id
where type='d' and backup_start_date =
(select top 1 backup_start_date from msdb..backupset
where @db = database_name and type = 'd'
order by backup_start_date desc)
-- Did a full database backup name get found
if @physical_device_name <> ""
begin
-- Build command to place a record in table that holds backup names
select @cmd = 'insert into ##backupnames values (' + char(39) +
@physical_device_name + char(39) + ',' + char(39) + @db
+ char(39) + "," +
char(39) + 'd' + char(39)+ ')'
-- Execute command to place a record in table that holds backup names
exec sp_executesql @cmd
end
-- Subsection 1B ----------------------------------------------
-- Reset the physical device name
set @physical_device_name = ""
-- Find the last differential database backup
select @physical_device_name = physical_device_name,
@backup_start_date = backup_start_date
from msdb..backupset a join msdb..backupmediaset b on a.media_set_id
= b.media_set_id
join msdb..backupmediafamily c on a.media_set_id =
c.media_set_id
where type='i' and backup_start_date =
(select top 1 backup_start_date from msdb..backupset
where @db = database_name and type = 'I' and
backup_start_date > @backup_start_date
order by backup_start_date desc)
-- Did a differential backup name get found
if @physical_device_name <> ""
begin
-- Build command to place a record in table that holds backup names
select @cmd = 'insert into ##backupnames values (' + char(39) +
@physical_device_name + char(39) + ',' + char(39) + @db
+ char(39) + "," +
char(39) + 'i' + char(39)+ ')'
-- Execute command to place a record in table that holds backup names
exec sp_executesql @cmd
end
-- Subsection 1B ----------------------------------------------
-- Build command to place records in table to hold backup names for
all
-- transaction log backups from the last database backup
set @CMD = "insert into ##backupnames select physical_device_name," +
char(39) + @db + char(39) +
"," + char(39) + "l" + char(39) +
"from msdb..backupset a join msdb..backupmediaset b on
a.media_set_id = b.media_set_id " +
"join msdb..backupmediafamily c on a.media_set_id = c.media_set_id "
+
"where type=" + char(39) + "l" + char(39) + "and
backup_start_date > @backup_start_dat and" +
char(39) + @db + char(39) + " = database_name"
-- Execute command to place records in table to hold backup names
-- for all transaction log backups from the last database backup
exec sp_executesql @cmd,@params=N'@backup_start_dat datetime',
@backup_start_dat = @backup_start_date
-- get next database to process
fetch next from db into @db
end
-- close and deallocate database list cursor
close db
deallocate db
-- Section B -------------------------------------------------------
-- define cursor for all database and log backups
declare backup_name cursor for
select name,type from ##backupnames
-- Open cursor containing list of database backups for specific
database being processed
open backup_name
-- Get first database backup for specific database being processed
fetch next from backup_name into @physical_device_name, @type
-- Set counter to track the number of backups processed
set @NUM_PROCESSED = 0
-- Process until no more database backups exist for specific database
being processed
WHILE @@FETCH_STATUS = 0
BEGIN
-- print file name to restore
print @physical_device_name
-- Get next database backup to process
fetch next from backup_name into @physical_device_name, @type
end
-- Close and deallocate database backup name cursor for current
database being processed
close backup_name
deallocate backup_name

-- Drop global temporary table
drop table ##backupnames

GO
Jul 20 '05 #1
0 1971

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: butatista | last post by:
Please help SQL Server 2000 Standard I am trying to restore databases on my server and am unable to get them to restore using the database...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
12
by: Bill Nguyen | last post by:
What's the VB syntax to run the CR report using the following SP? I use CrystalreportViewer and ReportDocument. Thanks Bill Here's the SP in...
22
by: M K | last post by:
Heres my SP: ( i am trying to add more than 1 field but get the same error no matter how many i try to add, i thought i would try to insert the...
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored...
13
by: Marcin Wasilewski | last post by:
Hi, How to call procedure, which is on SQL Server from Access using visual basic? Thanks, mw
5
by: Chris Cowles | last post by:
I use an application that uses Oracle 8.1.7. All functions of the application are completed with calls to stored procedures. A data entry error...
3
by: lenygold via DBMonster.com | last post by:
Here is the SP: --#SET TERMINATOR ! CREATE PROCEDURE execute_immediate (IN in_stmt VARCHAR(1000) ,OUT out_sqlcode INTEGER) LANGUAGE SQL MODIFIES...
0
debasisdas
by: debasisdas | last post by:
The following sample code is designed to display the use of accepting a list of values in a single parameter and process the same in the where clause...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.