如何捕获plsql输出值

时间:2014-03-10 09:00:16

标签: oracle plsql vb6

我是VB的新手,我想知道如何从oracle命令中捕获输出值。

我收到了这段代码:

Dim paramRet
Dim output

Sub function()

sql = "DECLARE
 variable := VARCHAR(10);

 variable := 'TEST';

BEGIN
 :=output := variable;
END; "

   Connect2Oracle    

    ExecuteSQLStatement sql  

    Set paramRet = adoCmd.CreateParameter("output",202,2,"20")
    adoCmd.Parameters.Append paramRet  

//print paramRet..

但是我的paramRet没有得到任何返回值。我错过了什么?

编辑:注意到我在CreateParameter中没有引用我的查询“sql”是我的观察正确吗?

1 个答案:

答案 0 :(得分:1)

你遗漏了几件事。首先,匿名过程没有任何返回值或out参数。因此,要获得返回值,您需要在Oracle数据库中创建一个函数。

示例:

PL / SQL:

Function Add5 (num in number) RETURN NUMBER IS
   Begin
   Return (num + 5);
End Add5;

VB:

Dim cmd As New ADODB.Command
Dim InputParam As New ADODB.Parameter
Dim ReturnParam As New ADODB.Parameter

cmd.ActiveConnection = myconnection
cmd.CommandText = "Add5"
cmd.CommandType = adCmdStoredProc
Set InputParam = cmd.CreateParameter("Prm1", adSmallInt, adParamInput, , 30)

'Using adParamOutPut instead of adParamReturnValue will result in the 
'following error:
'ORA-24334 - no descriptor for this position
'Set Prm2 = cmd.CreateParameter("Prm2", adSmallInt, adParamOutput) ' This is how you would use an out parameter
Set ReturnParam = cmd.CreateParameter("Prm2", adSmallInt, adParamReturnValue)

'You will also get the ORA-24334 error if you don't Append the parameters
'in the correct order. Make sure to bind the Returning parameter first.
cmd.Parameters.Append ReturnParam
cmd.Parameters.Append InputParam

cmd.Execute
MsgBox "Input Value = " & cmd.Parameters(1)
MsgBox "Return Value = " & cmd.Parameters(0)

Example from Ask Tom