VBA从存储过程获取输出参数

时间:2018-10-10 10:12:35

标签: excel-vba tsql

我有这个代码

 ' Insert item into database
    With cmd
        .ActiveConnection = conn

        .CommandText = "exec myschema.usp_procedure" _
        & "'" & user & "'," _
        & "''" ' output id

        .CommandType = adCmdText
        .CommandTimeout = 300
        .Execute

        Dim outputId As Integer
        outputId = cmd.Parameters("outputId").Value

    End With

我遇到错误

Item cannot be found in the collection corresponding to the requested name or ordinal

怎么了?如何获取@outputId int OUTPUT参数?

  • 存储过程执行正确,我可以在数据库中看到新项目

另一种尝试

Dim outputId As Integer
     With cmd
            .ActiveConnection = conn

            .CommandText = "exec edi.usp_ImportOrderHeader" _
            & "'" & user & "'," _
            & "''"

            .Parameters.Append .CreateParameter("outputId", adNumeric, adParamOutput, , outputId)

            .CommandType = adCmdText
            .CommandTimeout = 300

            .Execute
        End With

存储过程执行无错误,但outputId为零:/

0 个答案:

没有答案