我有以下代码在Sybase中通过VBA执行存储过程。
Sub GetInfo()
Dim rstRUB As ADODB.Recordset
Dim strRUB As String
Dim strcnn As String
Dim productType As String
Dim DealId As String
strcnn = "DSN=...;DATABASE=...;UID=...;PWD=...;"
Set cnn = New ADODB.Connection
cnn.Open strcnn
Set rstRUB = New ADODB.Recordset
Set ws = Workbooks("BODN_CPOTC.xls").Sheets("BODN_CPOTC")
Dim row As Long
row = 5
While ws.Cells(row, 1) <> ""
productType = ws.Cells(row, 1)
DealId = ws.Cells(row, 3)
Set cmd = New ADODB.Command
With cmd
.CommandText = "[Proc_BO_Deriv_AFOFLD]"
.ActiveConnection = cnn
.CommandType = adCmdStoredProc
'.Parameters.Append .CreateParameter("@valueD", adDate, adParamInput, , strDate)
'.Parameters.Append .CreateParameter("@maturityD", adDate, adParamInput, , Format$("20100504", "YYYYMMDD"))
.Parameters.Append .CreateParameter("@tipoProduto", adVarChar, adParamInput, 9, productType)
.Parameters.Append .CreateParameter("@dealId", adInteger, adParamInput, 0, DealId)
End With
rstRUB.Open cmd.Execute
If rstRUB.EOF = False Then
ws.Cells(row, 5) = rstRUB.Fields(0).Value
ws.Cells(row, 6) = rstRUB.Fields(1).Value
ws.Cells(row, 7) = rstRUB.Fields(2).Value
ws.Cells(row, 8) = rstRUB.Fields(3).Value
ws.Cells(row, 9) = rstRUB.Fields(4).Value
ws.Cells(row, 10) = rstRUB.Fields(5).Value
ws.Cells(row, 11) = rstRUB.Fields(6).Value
ws.Cells(row, 12) = rstRUB.Fields(7).Value
ws.Cells(row, 13) = rstRUB.Fields(8).Value
ws.Cells(row, 14) = rstRUB.Fields(9).Value
ws.Cells(row, 15) = rstRUB.Fields(10).Value
ws.Cells(row, 16) = rstRUB.Fields(11).Value
ws.Cells(row, 17) = rstRUB.Fields(12).Value
ws.Cells(row, 18) = rstRUB.Fields(13).Value
ws.Cells(row, 19) = rstRUB.Fields(14).Value
ws.Cells(row, 20) = rstRUB.Fields(15).Value
ws.Cells(row, 21) = rstRUB.Fields(16).Value
ws.Cells(row, 22) = rstRUB.Fields(17).Value
ws.Cells(row, 23) = rstRUB.Fields(18).Value
ws.Cells(row, 24) = rstRUB.Fields(19).Value
ws.Cells(row, 25) = rstRUB.Fields(20).Value
ws.Cells(row, 26) = rstRUB.Fields(21).Value
ws.Cells(row, 27) = rstRUB.Fields(22).Value
ws.Cells(row, 28) = rstRUB.Fields(23).Value
ws.Cells(row, 29) = rstRUB.Fields(24).Value
ws.Cells(row, 30) = rstRUB.Fields(25).Value
ws.Cells(row, 31) = rstRUB.Fields(26).Value
ws.Cells(row, 32) = rstRUB.Fields(27).Value
Else
ws.Cells(row, 5) = "Deal Not Found"
End If
row = row + 1
Wend
End Sub
第一个存储过程执行效果很好,但是当第二个存储过程执行时
rstRUB.Open cmd.Execute
程序没有结束..
我已经切换了参数(第一个是第二个),程序不会在第二次结束。
你知道我做错了什么吗?
提前致谢!
答案 0 :(得分:1)
尝试set rstRUB = cmd.execute
而不是rstRUB.Open cmd.Execute
答案 1 :(得分:1)
不确定为什么Set rstRUB = cmd.Execute
会产生编译错误......
您也可以尝试这样的电话:
rstRUB.Open cmd
无需在Open命令中调用cmd的execute方法,因为Open命令会触发查询执行。以下是记录集打开命令的链接:http://msdn.microsoft.com/en-us/library/ms675544(VS.85).aspx
在增加行数以准备下一个循环后,可能还有助于清除cmd和rstRUB对象的内存。
Set cmd = Nothing
Set rstRUB = Nothing
答案 2 :(得分:0)
我建议您为命令对象设置timeout属性:
http://msdn.microsoft.com/en-us/library/ms678265(v=VS.85).aspx
至少你会得到你的申请。
我怀疑您的参数中存在错误 - 名称,类型或您传递的参数数量;或者更糟糕的是,SQL中存在错误。不幸的是,一些OLEDB数据库提供程序及其相关驱动程序没有很好地实现错误处理,我怀疑除了“超时”消息之外,你不会从Connection对象的错误集合中获得任何内容。
因此,您将完成捕获SQL和参数集合的任务,并将其粘贴到您为数据库服务器获得的任何SQL开发窗口中:对于VBA中的错误,这是非常非常常见的在尝试手动运行SQL期间,将无形地更正参数集。