从VBA ADODB调用Oracle存储过程

时间:2011-03-23 17:21:01

标签: oracle stored-procedures adodb word-vba

您好
我试图使用WORD VBA中的存储过程来使用存储过程检索一些地址来填充列表字段。

Private Sub txtCpny_AfterUpdate()  
Dim rst As ADODB.Recordset  
Dim cmd As ADODB.Command
Dim param1 As ADODB.Parameter  
Dim param2 As ADODB.Parameter  
Dim strCpny As String  

strCpny = GetSearchString(Me.txtCpny) 'ie %Name%  

Set cmd = CreateObject("ADODB.Command")  

With cmd  
  .ActiveConnection = mcn  
  .CommandText = "LISTPARTNER_NAME"  
  .CommandType = adCmdStoredProc  
  Set param1 = .CreateParameter("RCT1", adInteger, adParamInputOutput, , Null)  
  Set param2 = .CreateParameter("firmaName", adVarChar, adParamInput, 50, strCpny)  
  .Parameters.Append param1    
  .Parameters.Append param2    
  Set rst = .Execute  
End With  

...Using the recordset here  

rst.Close  
Set param1 = Nothing  
Set param2 = Nothing  
Set cmd = Nothing  
End Sub

存储过程如下所示:SQL应该产生一个包含匹配公司的记录集。

PROCEDURE LISTPARTNER_NAME (  
firmaName    IN   VARCHAR2 DEFAULT NULL,  
RCT1         IN OUT GLOBALPKG.RCT1
)  

AS  
BEGIN  
  OPEN RCT1 FOR  
  SELECT  
  ...  
  FROM  
  ...  
  WHERE  
  KNAG.NAME_ORG LIKE LISTPARTNER_NAME.firmaName  
  ...  
END LISTPARTNER_NAME

执行命令时,VB抛出运行时错误

ORA-06550: Line 1, column 13:  
PLS-00306: wrong number or types of arguments in call to  
'LISTPARTNER_NAME'  
ORA-06550: line 1, column 7:  
PL/SQL: Statement ignored

在我看来,我对参数做错了。我一直在尝试各种版本的设置参数,没有运气 任何线索? 感谢

2 个答案:

答案 0 :(得分:1)

看看这个帖子,它可能会有所帮助 http://forums.oracle.com/forums/thread.jspa?threadID=360922

我唯一可以建议的是切换参数的顺序(因此它们的顺序相同 - 我知道ODP默认订购但我不确定ADODB)

答案 1 :(得分:1)

我已在Access中成功使用此代码:

Function runAdo(sql As String, usr As String, pwd As String)
'by Patrick Honorez - www.idevlop.com  ----- 09-nov-2012
'Purpose   :  run Oracle proc using ADO connection

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Set cn = New ADODB.Connection
    cn.Open GetPersonalizedConnectStringADO(usr, pwd)
    Set rs = New ADODB.Recordset
    rs.Open sql, cn, adOpenStatic, adLockReadOnly
    cn.Close
End Function

我很少使用Access中的ADO,因为我发现DAO更易于使用,但在这种情况下我必须执行一些需要不同UID的Oracle proc,并创建一个新的DAO Querydef,指定不同的UID和PWD,不起作用,也许是因为Access保留了连接的缓存 所以我决定将ADO用于第二个“用户”,它就像一个魅力。