Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Define a Variable for a cycle

Status
Not open for further replies.

Mharugha

Instructor
Oct 16, 2015
3
PT
Good day,
first of all, I'm still doing my first steps on Scripting and for this reason I apologize for any basic question.
I have the following script to Backup some SQL Databases. As I'm going to use this script in several computers I need to change that
3 first variables depending on which computer I will execute it.
The first 2 Variables are running nice but the third one is generating errors. I have already tried to use "" but nothing helped me.
Thank you in advance for you cooperation.

serverName = "SERVER\NTSQL2014"
backupDirectory = "D:\NTBckps\DBS\"
[highlight #EF2929]excludeDBS[/highlight] = rs("name") <> "tempdb" and rs("name") <> "NDatabase15"

Set objShell = wscript.createObject("wscript.shell")
if serverName = "" then wscript.quit
if backupdirectory = "" then wscript.quit
if not mid(backupDirectory,len(backupDirectory),1)="\" then
backupDirectory = backupDirectory & "\"
end if
set cn = createobject("ADODB.Connection")
set cmd = createobject("ADODB.Command")
set rs = createobject("ADODB.RecordSet")
cn.open "Provider=SQLOLEDB.1;Data Source=" & serverName & ";Integrated Security=SSPI;Initial Catalog=Master"
cmd.activeconnection = cn
cmd.commandtext = "exec sp_helpdb"
set rs = cmd.execute

while rs.eof <> true and rs.bof <> true
if rs("name") <> "tempdb" and rs("name") <> "NDatabase15" Then
'---> I would like to replace with someting like this: If excludeDBS Then <---'

backupDatabase rs("name")
end
if rs.movenext
wend
cn.close
wscript.echo "Backup Complete"

sub backupDatabase(byval databaseName)
fileName = backupDirectory & "NT_" & replace(replace(now,"/","_"),":","_") & "_" & databaseName & ".bak"
set cmdbackup = createobject("ADODB.Command")
cmdbackup.activeconnection = cn
cmdbackup.commandtext = "backup database " & databaseName & " to disk='" & fileName & "'"
cmdbackup.execute
end sub


Thank you in advance for your cooperation
 
I don't think you can get what you want. You'll need to have an IF-Then statement to establish what excludeDBS would be.

Alternately, if you don't want an If-Then Statement, get rid of your ExcludeDBS on line 3 and the If - Then with the rs("name") and replace with the following While
Code:
While if rs("name") <> "tempdb" and rs("name") <> "NDatabase15"
 
The problem is that you are trying to access rs("name") before you have even defined the recordset. So you need an IF statement, and it has to be below the set rs = cmd.execute line.

Why do you want this done in the first few lines? Is it because the name "NDatabase15" will change, so it's easier to modify if they are at the top of the code?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top