ADODB没有传入参数

时间:2013-12-12 14:32:49

标签: sql sql-server-2008 vba stored-procedures adodb

我有一个带有以下参数的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连接时,该过程未正确执行,因为我没有得到我应该得到的结果。有人能引导我朝着正确的方向前进吗?我错过了一些执行声明吗?

1 个答案:

答案 0 :(得分:1)

而不是:

cmd.Execute
Rs.Open cmd

尝试

Set rs = cmd.Execute

有关详细示例,请参阅How To Invoke a Stored Procedure with ADO Query Using VBA/C++/Java