Dim cnTest As New ADODB.Connection
Dim cmdTest As New ADODB.Command
cnTest.Open "Provider=sqloledb;Server=.;Database=数据库名;Uid=sa;Pwd=;"
'返回参数必须在前,SQL存储过程中没有对应的返回参数,只有对应的输入参数
cmdTest.Parameters.Append cmdTest.CreateParameter("P_Return", adInteger, adParamReturnValue, 4, 0)
cmdTest.Parameters.Append cmdTest.CreateParameter("P_Input01", adInteger, adParamInput, 4, 0)
cmdTest.Parameters.Append cmdTest.CreateParameter("P_Input02", adInteger, adParamInput, 4, 0)
cmdTest.ActiveConnection = cnTest
cmdTest.CommandText = "P_Temp" 'P_Temp是存储过程的名称
cmdTest.CommandType = adCmdStoredProc
cmdTest.Execute
Debug.Print cmdTest.Parameters("P_Return").Value '返回的值
**SQL中的存储过程代码**:
Create PROCEDURE dbo.P_Temp(@P_Input1 INT,@@P_Input2 INT)
AS
--代码段
Return 1 --返回值
GO