473,699 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_devic e_name nvarchar(128)
declare @backup_start_d ate 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..sysdata bases
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_devic e_name = ""
-- get the name of the last full database backup
select @physical_devic e_name = physical_device _name ,
@backup_start_d ate = backup_start_da te
from msdb..backupset a join msdb..backupmed iaset b on a.media_set_id
= b.media_set_id
join msdb..backupmed iafamily c on a.media_set_id =
c.media_set_id
where type='d' and backup_start_da te =
(select top 1 backup_start_da te from msdb..backupset
where @db = database_name and type = 'd'
order by backup_start_da te desc)
-- Did a full database backup name get found
if @physical_devic e_name <> ""
begin
-- Build command to place a record in table that holds backup names
select @cmd = 'insert into ##backupnames values (' + char(39) +
@physical_devic e_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_devic e_name = ""
-- Find the last differential database backup
select @physical_devic e_name = physical_device _name,
@backup_start_d ate = backup_start_da te
from msdb..backupset a join msdb..backupmed iaset b on a.media_set_id
= b.media_set_id
join msdb..backupmed iafamily c on a.media_set_id =
c.media_set_id
where type='i' and backup_start_da te =
(select top 1 backup_start_da te from msdb..backupset
where @db = database_name and type = 'I' and
backup_start_da te > @backup_start_d ate
order by backup_start_da te desc)
-- Did a differential backup name get found
if @physical_devic e_name <> ""
begin
-- Build command to place a record in table that holds backup names
select @cmd = 'insert into ##backupnames values (' + char(39) +
@physical_devic e_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..backupmed iaset b on
a.media_set_id = b.media_set_id " +
"join msdb..backupmed iafamily c on a.media_set_id = c.media_set_id "
+
"where type=" + char(39) + "l" + char(39) + "and
backup_start_da te > @backup_start_d at 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_d at datetime',
@backup_start_d at = @backup_start_d ate
-- 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_devic e_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_devic e_name
-- Get next database backup to process
fetch next from backup_name into @physical_devic e_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 2068

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

Similar topics

3
2250
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 option. For example, the location of my backup file is: D:\MSSQL\BACKUP\MY_DB\MY_DB_ddmmyy.bak
2
5451
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
12
10407
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 SQLserver 2K: CREATE proc mysp_ReportSubmission @salesdate as varchar(20),
22
4275
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 primary key only and work up from there but the primary key insert didnt work either. I can run the SP manually in Visual Studio .NET and it works. When i try to do this it doesn't. Any help is greatly appreciated... newbie here as well so please be gentle :) ALTER PROCEDURE dbo.AddJob
2
3146
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 Procedure Builder is not enabled on Java stored procedures. It is enabled for SQL stored procedures. It is possible to "Build" and "Run" the Java SPs, it just isn't possible to click on the "Build for Debug" option. Thanks for any help in advance. Michael
13
2312
by: Marcin Wasilewski | last post by:
Hi, How to call procedure, which is on SQL Server from Access using visual basic? Thanks, mw
5
2262
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 occurred that caused thousands of records to be created with a consistent error in a single field. I can identify those easily records with a select statement. I'd *really* rather not have to change them all manually. I do have access to run a simple update query to correct only the field in...
3
3780
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 SQL DATA BEGIN DECLARE sqlcode INTEGER; DECLARE EXIT HANDLER FOR sqlexception SET out_sqlcode = sqlcode;
0
6412
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 inside a procedure. STEP1:-First create an user defined type . CREATE OR REPLACE TYPE SPLIT_TBL AS TABLE OF VARCHAR2(32767); STEP2:-Create a function to split the list of values in a single parameter. CREATE OR REPLACE FUNCTION SPLIT (
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.