tags:

views:

248

answers:

1

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.

A: 

It seems as though 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.

René Nyffenegger