Arno Conradie
Arno Conradie

Reputation: 11

How to Execute an Oracle SQL Statements with VBScript

I am trying to execute an Oracle SQL statement or Oracle Functions through Microsoft VBScript and the loop throught the result set or display the value returned by the function

So far I have managed to connect to Oracle via SQLPlus*, but now I am stuck. Can anybody help?


Dim output

Dim WshShell, oExec, input 

set WshShell = CreateObject("WScript.Shell")

set oEnv=WshShell.Environment("Process") 

cmdString = "C:\Oracle\11g\product\11.1.0\ruby\BIN\sqlplus.exe -S stradmin/stradmin@ruby select * from dual"
Set oExec = WshShell.Exec(cmdString) 


WScript.Echo "Status" & oExec.Status


Do While oExec.Status = 0 

     WScript.Sleep 2 

Loop 



input = "" 



Do While Not oExec.StdOut.AtEndOfStream 

          input = input & oExec.StdOut.Read(1) 

Loop 



wscript.echo input 

Upvotes: 1

Views: 21022

Answers (1)

Tester101
Tester101

Reputation: 8172

Try this it should add each field in the recordset to the input string. If you only want a specific value from each record you can do this

input = input & rs.Fields.Item("FIeld_Name")

Instead of looping though each field.

connectionString = "DRIVER={Microsoft ODBC for Oracle};SERVER=oracle_server;User Id=user;Password=password;"

Set connection = CreateObject("ADODB.Connection")

connection.Open connectionString
Set rs = connection.Execute("select * from dual")

input = ""

Do Until rs.EOF
    for i = 0  To rs.Fields.Count - 1
        input = input & rs.Fields.Item(i) & "|"
    Next
    input = input & VBNewLine
    rs.MoveNext
Loop

MsgBox input

Set connection = Nothing
Set rs = Nothing

Upvotes: 3

Related Questions