473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Batch script for database restoration

1
Total newbie here, many apologies if I have failed to find an extant answer to this question.
Using:
SQL Server 2000

I need to restore several hundred databases from an external drive to local drive.
Would like to batch, eg, call on a text file listing old and new db names/paths and loop through to end of text doc.

Currently have non-batching script:

Expand|Select|Wrap|Line Numbers
  1. DECLARE 
  2. @olddir varchar(200), @olddb varchar(200),
  3. @newdir varchar(200), @dbname varchar(200),
  4. @dbdata varchar(200), @dblog varchar(200),
  5. @oldfile varchar(200), @newfile varchar(200), @logfile varchar(200)
  6.  
  7. SET @olddir = 'E:\DATA\SQLdbBAKs\'
  8. SET @olddb = 'mydb_xxxxx.BAK'
  9. SET @newdir = 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\'
  10. SET @dbname = 'mydb'
  11. SET @dbdata = @dbname + '_Data.mdf'
  12. SET @dblog = @dbname + '_Log.ldf'
  13. SET @oldfile = @olddir + @olddb
  14. SET @newfile = @newdir + @dbdata + '_Data.mdf'
  15. SET @logfile = @newdir + @dblog + '_Log.ldf'
  16.  
  17. RESTORE DATABASE @dbname
  18.     FROM DISK = @oldfile WITH FILE = 1,
  19.     MOVE @dbdata TO @newfile,
  20.     MOVE @dblog TO @logfile
  21.  
  22.  
So
1) is there a way to loop through E:\DATA\SQLdbBAKs\, grab all the dbs and restore them locally
OR
2) if i had a text file:

Expand|Select|Wrap|Line Numbers
  1. olddb,newdb,olddir,newdir
  2. mydb_xxxxx, mydb, E:\DATA\SQLdbBAKs\, C:\Program Files\Microsoft SQL Server\MSSQL\Data\
  3.  
could i loop through the rows and assign column entries to my variables?

Thank you for help!
-d
Dec 13 '11 #1
1 2784
ck9663
2,878 Expert 2GB
Yes.

1. Use xp_cmdshell to get the content of the folder. You can store the result into a table.
2. Loop through that table. Each row of that table represents a file (filter out the other text as result if dir *.* command).

Happy Coding!!!


~~ CK
Dec 14 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Jeff Lambert | last post by:
We have a Windows client application written in delphi that connects to Oracle 8i w/ ADO. We accumulate a lot of SQL statements in a loop and finally send the strings list to be executed. What I...
3
by: John McGinty | last post by:
simple as that really. I want to run a batch script when a button in pressed. The script is to run a DTS package (thats another issue entirely) to refresh some data which is all working just fine....
0
by: py | last post by:
I am trying to execute a batch script on a remote computer. The batch script looks like: @echo off start c:\python24\python.exe c:\a_script.py Here's the setup: Computer A (my computer),...
1
by: Thyagu | last post by:
Hi, Does a database restoration perform any update-statistics/defragmentation by default. We observed a marked improvement in performance when we restored the a database from an high end...
0
by: sambasiva | last post by:
hi, i have one .csv file and one server.my requirement is csv file server abc_gw.pdf ...
0
by: bharadwaj | last post by:
hi, i am trying to do master restoration using the following steps. 1.net stop MSSQLServer 2.sqlservr -m 3.use MASTER go RESTORE DATABASE master FROM disk='master.BAK' replace, MOVE...
1
by: jschmidt | last post by:
I am wondering if there is a way to zip a (one) file using a batch script. I have an access database that needs to be zipped on a regular basis because many people download this file from a website. ...
1
by: Rajeswary | last post by:
I need to copy an excel doc in the same directory with a new name.. That is I have got an excel file abd_Week 37 and I need to run a batch script to copy the same file to the same directory with...
3
by: zuldera7 | last post by:
Hi, I know that you can output script information or batch file information into a txt file, but is it possible to direct to a database table?
5
by: veeraiah | last post by:
Hi, I am new to PERL Scripting, i need your help with one of the problem i am having. I am having a PERL Script which has two variables $CurMon and $PriorMon. a Windows batch script calls this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.