Passing SQLPlus script location using Windows Script Host Object Model

S

Scott Spence

Ok,

So I haz lots of scripts that are generated by a team in my organisation which need to then be processed via SQLPlus

Basically we/me get loads of emails with the script location @Z:/aFolder/aScript.sql;

We then have to process them individually by opening up SQLPlus and pasting the file location in there "@Z:/aFolder/aScript.sql;"

I have been looking into a way to automate this via either c# or VBA

I think that the Windows Script Host Object Model option via VBA is a good one

Example:


Option Explicit

Sub SQLPlus(strFilePath)

Dim WShell As New WshShell
WShell.Run "sqlplus username/[email protected] " & strFilePath

End Sub

Sub test()

Call SQLPlus("@Z:/aFolder/aScript.sql;")

End Sub

Only issue is that I get an error from passing that last file location in there: "SP2-0310: unable to open file location "Z:/aFolder/aScript.sql;"

Am I missing a special character or something from this?

I pass the @ sign into the string but its not recognised on the command line??

Any input greatly appreciated, thanks.
 
J

John Coleman

Ok,

So I haz lots of scripts that are generated by a team in my organisation which need to then be processed via SQLPlus

Basically we/me get loads of emails with the script location @Z:/aFolder/aScript.sql;

We then have to process them individually by opening up SQLPlus and pasting the file location in there "@Z:/aFolder/aScript.sql;"

I have been looking into a way to automate this via either c# or VBA

I think that the Windows Script Host Object Model option via VBA is a good one

Example:

Option Explicit

Sub SQLPlus(strFilePath)

    Dim WShell As New WshShell
    WShell.Run "sqlplus username/[email protected] " & strFilePath

End Sub

Sub test()

    Call SQLPlus("@Z:/aFolder/aScript.sql;")

End Sub

Only issue is that I get an error from passing that last file location inthere: "SP2-0310: unable to open file location "Z:/aFolder/aScript.sql;"

Am I missing a special character or something from this?

I pass the @ sign into the string but its not recognised on the command line??

Any input greatly appreciated, thanks.

Sometimes enclosing paths in strings helps. Maybe you can try

Sub SQLPlus(strFilePath)

Dim WShell As New WshShell
WShell.Run "sqlplus ""username/[email protected] " &
strFilePath & """"
End Sub
 
S

Scott Spence

Good idea John, tried it and the window just closes immediately

Thanks for the suggestion :)
 
T

Tim Williams

Is the sqlplus window supposed to stay open ?

Is there an "exit" at the end of the script you're calling ?

Tim
 
T

Tim Williams

If there's an exit at the end of your script then that would explain
why the sqlplus window closed. If you comment that out does it stay
open?

Tim
 
S

Scott Spence

It doesn't even open the script in SQLPlus

Like I said in the main post I get an error: "SP2-0310: unable to open file location "Z:/aFolder/aScript.sql;"

I have validated the location and file exist, it just looks like the parameter isn't passed in correctly for some reason

If I just paste in the file location: @Z:/aFolder/aScript.sql;

It runs fine but when I try pass it I get the error message without the @ in the command string "SP2-0310: unable to open file location "Z:/aFolder/aScript.sql;"
 
J

John Coleman

It doesn't even open the script in SQLPlus

Like I said in the main post I get an error: "SP2-0310: unable to open file location "Z:/aFolder/aScript.sql;"

I have validated the location and file exist, it just looks like the parameter isn't passed in correctly for some reason

If I just paste in the file location: @Z:/aFolder/aScript.sql;

It runs fine but when I try pass it I get the error message without the @in the command string "SP2-0310: unable to open file location "Z:/aFolder/aScript.sql;"

Try

Dim WShell As New WshShell
WShell.Run "sqlplus username/[email protected] " & " "
& strFilePath
End Sub

The only addition from your first script is to put a space between the
sql server path and the script that you are trying to execute

-John
 
S

Scott Spence

There's already a space at the end of the server location

*WShell.Run "sqlplus username/[email protected] "*

But just to humour you I have added the extra space and I still get the same error

Thanks anyway :)
 
S

Scott Spence

There's already a space at the end of the server location

*WShell.Run "sqlplus username/[email protected] "*

But just to humour you I have added the extra space and I still get the same error Thanks anyway :)
 
S

Scott Spence

Ok, this is now resolved [in a fashion]

What I'm doing is passing the file location parameter to a .bat file

So I have my .bat file OracleSQL.bat which contains this code:

@echo off
sqlplus username/[email protected] @%1
exit;

I can now shell out to the batch file with the file parameter:

Dim strBatchName As String

strBatchName = "C:\location\of\bat\OracleSQL.bat Z:/aFolder/aScript.sql"
Shell strBatchName

Done :)
 
J

John Coleman

Ok, this is now resolved [in a fashion]

What I'm doing is passing the file location parameter to a .bat file

So I have my .bat file OracleSQL.bat which contains this code:

@echo off
sqlplus username/[email protected] @%1
exit;

I can now shell out to the batch file with the file parameter:

Dim strBatchName As String

strBatchName = "C:\location\of\bat\OracleSQL.bat Z:/aFolder/aScript.sql"
Shell strBatchName

Done :)

Glad that you found a work-around. I suspect that the problem lies in
the area of permissions. The fact that you can type a string at the
command prompt and have it work but not have identitical behavior when
you use the same string with the script host run method suggests that
something is preventing the script-host from accessing certain files.
Either that or the directory that the script-host is trying to run the
command in is a different directory from that where the .bat file runs
-- though you seem to be passing fully-qualified paths so I don't see
why that should be an issue.

As an aside - have you checked out Power Shell yet? I have been
dabbling in it recently and it really does seem to be more powerful
than VBScript - although I haven't yet figured out how to call it
seemlessly from VBA. A less kludgy solution to your problem would
probably be possible with Power Shell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top