我有一个带有以下参数的SQL SP …
myProc(@param1 AS TINYINT, @sparam2 AS TINYINT, @param3 AS TINYINT, @param4 AS TINYINT, @param5 AS TINYINT, @param6 AS TINYINT, @param7 AS TINYINT, @paramOut NVARCHAR (255) OUTPUT)
而且我在VBA中有以下ADODB代码…
sVal1 = 1 sVal2 = 1 sVal3 = 1 sVal4 = 1 sVal5 = 1 sVal6 = 1 sVal7 = 1 Dim cnn As ADODB.Connection Dim cmd As ADODB.Command Dim param1, param2, param3, param4, param5, param6, param7, param8 As ADODB.Parameter Dim Rs As ADODB.Recordset Set cnn = New ADODB.Connection cnn.ConnectionString = "DRIVER=SQL Server;SERVER=SERVER;DATABASE=PEER_DB;Trusted_Connection=Yes" cnn.Open cnn.ConnectionString Set cmd = New ADODB.Command Set Rs = New ADODB.Recordset cmd.ActiveConnection = cnn cmd.CommandType = adCmdStoredProc cmd.CommandText = "myProc" Set param1 = cmd.CreateParameter("@param1", adTinyInt, adParamInput, 1, sVal1) cmd.Parameters.Append param1 Set param2 = cmd.CreateParameter("@param2", adTinyInt, adParamInput, 1, sVal2) cmd.Parameters.Append param2 Set param3 = cmd.CreateParameter("@param3", adTinyInt, adParamInput, 1, sVal3) cmd.Parameters.Append param3 Set param4 = cmd.CreateParameter("@param4", adTinyInt, adParamInput, 1, sVal4) cmd.Parameters.Append param4 Set param5 = cmd.CreateParameter("@param5", adTinyInt, adParamInput, 1, sVal5) cmd.Parameters.Append param5 Set param6 = cmd.CreateParameter("@param6", adTinyInt, adParamInput, 1, sVal6) cmd.Parameters.Append param6 Set param7 = cmd.CreateParameter("@param7", adTinyInt, adParamInput, 1, adParamReturnValue) cmd.Parameters.Append param7 Set param8 = cmd.CreateParameter("@paramOut", adVarChar, adParamOutput, 255, adParamReturnValue) cmd.Parameters.Append param8 Rs.CursorType = adOpenStatic Rs.CursorLocation = adUseClient Rs.LockType = adLockOptimistic cmd.Execute Rs.Open cmd
我要做的只是传递VBA sVal变量并基于这些参数执行我的过程,然后我需要返回一直有效的输出参数。使用ADODB连接时,该过程未正确执行,因为我没有得到应有的结果。有人可以引导我朝正确的方向前进,我是否缺少一些执行语句?
代替:
cmd.Execute Rs.Open cmd
尝试
Set rs = cmd.Execute
有关详细的示例,请参见如何使用VBA / C ++ / Java使用ADO Query调用存储过程。