VBA调用带游标返回值的ORACLE存储过程

【解决方案一】:
On Error GoTo 11
Dim hisCNN As ADODB.Connection
Dim hisRST As ADODB.Recordset
Dim sSQL As String
Dim iZDS As Integer
Dim sBT As String
Dim sTEMP As String
Dim hisCMD As New ADODB.Command
Dim Para1 As New ADODB.Parameter
Dim Para2 As New ADODB.Parameter
Dim Para3 As New ADODB.Parameter


Set hisCNN = New ADODB.Connection
hisCNN.CursorLocation = adUseClient


hisCNN.Open "Provider=MSDASQL.1;Persist Security Info=False;User ID=i_pathology;pwd=i_pathology;Data Source=aaa"
MsgBox "连接数据库成功!"
//aaa是ODBC名


Set hisCMD.ActiveConnection = hisCNN
hisCMD.CommandType = adCmdStoredProc
hisCMD.CommandText = "zhi.F_get_sick_info"


Set Para1 = hisCMD.CreateParameter("as_patient_id", adVarChar, adParamInput, 100, "00")
Para1.Value = "3333333"
hisCMD.Parameters.Append Para1


Set Para2 = hisCMD.CreateParameter("as_io_flag", adVarChar, adParamInput, 100, "1")
Para2.Value = "1"
hisCMD.Parameters.Append Para2


MsgBox "hiscmd"
Set hisRST = hisCMD.Execute


MsgBox "得到记录集!"


如果把连接改成
Provider=msdaora.1;Data Source=服务名;User ID=i_pathology;Password=i_pathology;
的话,提示 参数数量和类型不对.第三个参数是 游标,我查了,应该不用赋值的.


【解决方案二】:
PL/SQL 代码:
CREATE OR REPLACE PACKAGE "SCOTT"."PKG_TEST" AS
       TYPE myrcType IS REF CURSOR;
       FUNCTION get(strbarcode VARCHAR) RETURN myrcType;
END pkg_test;


CREATE OR REPLACE PACKAGE BODY "SCOTT"."PKG_TEST" AS
    FUNCTION get(strbarcode IN VARCHAR) RETURN myrcType IS
      rc myrcType;
    BEGIN
      OPEN rc FOR strbarcode;
      RETURN rc;
    END get;
END pkg_test;
--------------------------------------------------------------------------------------------------------
VB 代码:
Private Sub Command1_Click()
On Error GoTo cursorErr:
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim cmd As New ADODB.Command
    
    cnn.ConnectionString = "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott;Data Source=oraAny;Extended Properties=PLSQLRSet=1"
    cnn.Open
    
    With cmd
        .ActiveConnection = cnn
        .CommandType = adCmdText
        .CommandText = "{CALL scott.pkg_test.get(?)}"
        .Parameters.Append .CreateParameter("strBarCode", adVarChar, adParamInput, 100, "SELECT * FROM TAB")
    End With
    
    rst.CursorType = adOpenStatic
    rst.LockType = adLockReadOnly
    Set rst.Source = cmd
    rst.Open
    
    MsgBox rst.RecordCount


    Set rst = Nothing
    Set cmd = Nothing
    Exit Sub


cursorErr:
    Set cmd = Nothing
    Set rst1 = Nothing
    MsgBox Err.Description
End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值