Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With ADO, how do I call an Oracle PL/SQL block and specify input/output bind variables (parameters?)

Tags:

oracle

vba

ado

I am trying to call a PL/SQL block with ADO and VBA, but I can't pass input and/or output bind variables (probably aka parameters).

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))

The snippet above fails at the cm.execute line with an ORA-01008: not all variables bound

I'd appreciate any help towards a solution for my problem.

like image 457
René Nyffenegger Avatar asked Mar 03 '10 17:03

René Nyffenegger


1 Answers

It seems as though the statement cannot start with a declare. (Thanks to Thomas Jones-Low for his valuable comment).

So, the statement must be enclosed in another begin .. end block:

' 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;"

Now, it works as expected.

like image 78
René Nyffenegger Avatar answered Sep 27 '22 16:09

René Nyffenegger