存储过程不返回记录集

时间:2016-12-20 19:16:30

标签: sql-server stored-procedures access-vba ms-access-2010

我被困在下面。我有一个存储过程,它以日期和经理ID作为参数来生成Resultset

CREATE PROCEDURE[ACT].[sp_getAllocations]
    @dtmReportDate DATE,
    @ManagerID BIGINT,
    @type nvarchar(MAX) OUTPUT
AS
BEGIN
    SELECT 
        ARCVTADMIN.tblEmployee.strFirstName + ' ' + ARCVTADMIN.tblEmployee.strSurname AS Name, 
        COUNT(ACT.tblActivity.employeeID) AS Workstate, 
        SUM(CASE WHEN ACT.tblactivity.statusTypeID = 4 AND ACT.tblActivity.dtmCompletedDate = @dtmReportDate THEN 1 ELSE 0 END) AS Completed, 
        SUM(CASE WHEN ACT.tblActivity.statusReasonID = 1 THEN 1 ELSE 0 END) AS NewLeads, 
        SUM(CASE WHEN ACT.tblContact.dtmLoggedDate = @dtmReportDate THEN 1 ELSE 0 END) AS Worked
    FROM            
        ACT.tblContact 
    RIGHT OUTER JOIN
        ACT.tblActivity ON ACT.tblContact.activityID = ACT.tblActivity.activityID 
    LEFT OUTER JOIN
        ARCVTADMIN.tblEmployee ON ACT.tblActivity.employeeID = ARCVTADMIN.tblEmployee.employeeID 
                               AND ACT.tblActivity.employeeID = ARCVTADMIN.tblEmployee.employeeID 
                               AND ACT.tblActivity.employeeID = ARCVTADMIN.tblEmployee.employeeID
                               AND ACT.tblActivity.employeeID = ARCVTADMIN.tblEmployee.employeeID
    GROUP BY 
        ARCVTADMIN.tblEmployee.strFirstName + ' ' + ARCVTADMIN.tblEmployee.strSurname, 
        ARCVTADMIN.tblEmployee.managerID
    HAVING        
        (ARCVTADMIN.tblEmployee.managerID = @ManagerID)
END

注意:ARCVTADMIN,ACT是架构名称

当我在SQL Server中运行

时,此存储过程正常工作
Declare @D nVarchar(max);
EXECUTE ACT.sp_getAllocations @dtmReportDate = '20161220', @ManagerID = 91, @type=@D out
Print @D

以下是我在SQL Server中执行它时存储过程产生的结果

Name            Workstate   Completed    NewLeads    Worked
===========================================================
Nikki Furnell     4999          0        3434           0
Harriet Johnson     10          0           0           0
Claire Rowe        138          0          17           0
Carina Hughes        4          0           0           0

我在类模块中有一个函数,如下所示

Public Function getSPRecordset(dtmReportDate As String, managerID As Long) As ADODB.Recordset
Dim strType As String
Dim rst As ADODB.Recordset
If Not (isConnectionOpen()) Then
OpenConnection
End If

Set ADOCom = New ADODB.Command
ADOCom.ActiveConnection = ADOConn
ADOCom.CommandType = adCmdStoredProc
ADOCom.CommandText = "[ACT].[sp_getAllocations]"
ADOCom.Parameters.Refresh

ADOCom.Parameters.Append ADOCom.CreateParameter("@dtmReportDate", adDate,   adParamInput, 10, dtmReportDate)
ADOCom.Parameters.Append ADOCom.CreateParameter("@ManagerID", adBigInt, adParamInput, 10, managerID)
ADOCom.Parameters.Append ADOCom.CreateParameter("@type", adVarChar, adParamOutput, 10000, strType)
Set rst = ADOCom.Execute
Set getSPRecordset = rst
End Function

我试图检索结果集并将其分配给我的表单,然后我尝试下面的内容。

Public Sub loadAllocation()
Dim objSS As clsSQLServer
Dim rst As ADODB.Recordset
Set objSS = New clsSQLServer
Set rst = objSS.getSPRecordset(Me.dtmReportDate, Me.managerID)
Set Me.frmTeamDashboardWorkstate.Form.Recordset = rst ***Error Line ****
set rst=nothing
End Sub

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

好的,我认为你需要做的是......

首先,您的Public Sub loadAllocation()是在一个单独的模块中,还是在父模式的模块中?我建议将其作为父表单模块中的Private Sub

其次,您需要在父表单模块中声明rst作为模块级变量。它必须在loadAllocation()运行后保持在范围内。所以在模块的顶部,在任何Sub或Function定义之前,在Option Compare DatabaseOption Explicit之后,添加

Private rst As ADODB.Recordset

然后我会将您的程序重新编码为

Private Sub loadAllocation()
On Error GoTo PROC_ERR

    Dim objSS As New clsSQLServer

    Set rst = New ADODB.Recordset
    rst.CursorLocation = adUseClient
    Set rst = objSS.getSPRecordset(Me.dtmReportDate, Me.managerID)
    'You could put some Debug.Print statements in here just to check that rst has retrieved some data'
    If rst.State = adStateOpen Then
        Set Me.frmTeamDashboardWorkstate.Form.Recordset = rst
        frmTeamDashboardWorkstate.Form.Requery
        rst.ActiveConnection = Nothing
    End If

PROC_EXIT:
    'Put any clean-up code in here'
    Exit Sub

PROC_ERR:
    'Put your preferred error handling code in here'
    Resume PROC_EXIT

End Sub

确保在关闭父窗体时(在Form_Unload事件中)清理并Set rst = Nothing

答案 1 :(得分:0)

您使用的是32位还是64位访问?如果你使用32位,那么bigint是64位,32位访问不能处理它。