Variable substitution in VBScript

Based on a recent email exchange with a colleague, it occurred to me that this might be handy to post as a blog entry.

The question - “Why do you need the added ‘&’ statement in VBScript for variables? “

When you are using variables in select statements, they have to properly resolve into the syntax that VBScript would understand.

To illustrate this example, review the following VBScript:

‘ begin script

strComputer = "GENIUS"

strSQL = "Select foo FROM table WHERE Name = 'strComputer'"

Wscript.Echo strSQL

strSQL = "Select foo FROM table WHERE Name = '" & strComputer & "'"

Wscript.Echo strSQL

‘ end script

Save it as with a VBS extension, then run it.  You should get these results:

clip_image001

clip_image002

Note: If you are using numeric variables, you would not include the quotes.

Published Thursday, January 27, 2011 10:12 AM by sthompson
Filed under: ,

Comments

No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems