如何使用VBScript执行Oracle SQL语句

时间:2009-07-09 13:10:35

标签: oracle vbscript sqlplus

我正在尝试通过Microsoft VBScript执行Oracle SQL语句或Oracle函数,并在结果集中循环或显示函数返回的值

到目前为止,我已经设法通过SQLPlus *连接到Oracle,但现在我被卡住了。有人可以帮忙吗?


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 

1 个答案:

答案 0 :(得分:3)

尝试这一点,它应该将记录集中的每个字段添加到输入字符串。如果您只想从每条记录中获取特定值,则可以执行此操作

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

而不是循环每个字段。

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