使用ADO,如何调用Oracle PL / SQL块并指定输入/输出绑定变量(参数?)

时间:2010-03-03 17:17:03

标签: oracle vba ado

我试图用ADO和VBA调用PL / SQL块,但是我无法传递输入和/或输出绑定变量(可能也就是参数)。

dim cn as ADODB.connection
' ... open connection ...

dim plsql as string

plsql =         "declare"
plsql = plsql & "  num_in  number := ?;"
plsql = plsql & "  num_out number; "
plsql = plsql & "begin"
plsql = plsql & "  num_out := num_in * 5;"
plsql = plsql & "  ? := num_out;"
plsql = plsql & "end;"

dim cm as ADODB.command
set cm = new ADODB.command
set cm.activeConnection = cn
cm.commandText = plsql
cm.commandType = adCmdText

cm.parameters.append cm.createParameter(, adDouble, adParamInput,, 5)
cm.parameters.append cm.createParameter(, adDouble, adParamOutput   )

cm.execute  ' FAILS HERE

msgBox(cm.parameters(2))

上面的代码段在cm.execute行失败了 ORA-01008:并非所有变量都绑定

我很感激为解决我的问题提供任何帮助。

1 个答案:

答案 0 :(得分:6)

似乎语句不能以declare开头。 (感谢Thomas Jones-Low的宝贵评论)。

因此,该声明必须包含在另一个begin .. end块中:

' additional begin so that the statement does not start with a declare:
plsql =         "begin "

plsql = plsql & "declare"
plsql = plsql & "  num_in  number := ?;"
plsql = plsql & "  num_out number; "
plsql = plsql & "begin"
plsql = plsql & "  num_out := num_in * 5;"
plsql = plsql & "  ? := num_out;"
plsql = plsql & "end;"

' closing the additional begin:
plsql = plsql & "end;"

现在,它按预期工作。