我现在坚持这个超过 48 小时。我正在尝试使用 ADO 对象检索输出参数值。这是我的 vba 代码。
Function ExecuteCommand(cSp As String, aParameters As Variant) As Integer
Dim lnReturn As Integer
Dim loCommand As ADODB.Command
Dim prm As ADODB.Parameter
Dim rds As ADODB.Recordset
Set loCommand = New ADODB.Command
loCommand.CommandText = cSp
loCommand.CommandType = adCmdStoredProc
Set loCommand.ActiveConnection = goConnection
loCommand.NamedParameters = True
Dim iCount As Integer
For iCount = LBound(aParameters) To UBound(aParameters)
If aParameters(iCount, 0) <> "" Then
If aParameters(iCount, 1) = adNumeric Then
Set prm = loCommand.CreateParameter(aParameters(iCount, 0), aParameters(iCount, 1), adParamInput)
prm.Precision = 18
prm.NumericScale = 0
prm.Value = aParameters(iCount, 3)
Else
Set prm = loCommand.CreateParameter(aParameters(iCount, 0), aParameters(iCount, 1), adParamInput, , aParameters(iCount, 3))
End If
loCommand.Parameters.Append prm
End If
Next
Set prm = loCommand.CreateParameter("@returnval", adInteger, adParamOutput, , lnReturn)
loCommand.Parameters.Append prm
Set rds = loCommand.Execute
' Check the value of lnReturn and it's zero here.
lnReturn = loCommand.Parameters("@returnval").Value
ExecuteCommand = lnReturn
End Function
我的存储过程如下...
USE [table_name]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spname]
@para1 int,
@para2 int,
@para3 int,
@returnval int Output
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM type_table WHERE type1=@para1 and type2=@para2 and type3=@para3
SET @returnval = @@ROWCOUNT
RETURN @returnval
END
这些是我的发现
lnReturn = 0 always
rds.EOF = False
rds.RecordCount = -1
我的问题是我没有得到正确的受影响行数,即 1。我不确定代码有什么问题,有人可以帮帮我吗?