使用自定义 SQL 查询时出现 PowerPivot 错误

PowerPivot Error When Using Custom SQL Query

我在 SQL Server Management Studio 2012 中构建了一个查询,它在测试时运行良好。但是,当尝试在 Power Pivot 中使用此代码时,我收到以下错误:

OLE DB 或 ODBC 错误。

An error occurred while processing table 'Query'.
The current operation was cancelled because another operation in the transaction failed.

对可能引发此错误的原因有任何想法吗?

这是我的查询:

DECLARE @beginDate as DATE;
DECLARE @endDate as DATE;
DECLARE @tempTable AS TABLE(BusinessUnitID INTEGER, BusinessUnitName VARCHAR(255), SkillCode VARCHAR(255), 
                    Actuals INTEGER, DateCounted DATE)

DECLARE @count as INTEGER;

SET @beginDate = (SELECT MAX(DateCounted) FROM mmapps.dbo.t_EMS_BM_ActualsHistory);
SET @endDate = DATEADD(week,1,@beginDate);
SET @count = 0;

WHILE DATEADD(week,@count,@beginDate) < DATEADD(year,1,@beginDate)
BEGIN
    IF (@count = 0)
    BEGIN
        INSERT INTO @tempTable (BusinessUnitID,BusinessUnitName,SkillCode, Actuals, DateCounted)
        SELECT a.BusinessUnitID, a.BusinessUnitName, a.SkillCode, a.TotalHeads + 
            (
            SELECT COUNT(*) 
            FROM mmapps.dbo.t_EMS_BM_ChartData AS b 
            WHERE a.BusinessUnitID = b.BusinessUnitID AND a.SkillCode = b.SkillCode 
                AND b.ActivityTypeID BETWEEN 6 AND 10 AND b.SkillCode IS NOT NULL
                AND b.Date < @endDate) -
            (
            SELECT COUNT(*) 
            FROM mmapps.dbo.t_EMS_BM_ChartData AS b 
            WHERE a.BusinessUnitID = b.BusinessUnitID AND a.SkillCode = b.SkillCode 
                AND b.ActivityTypeID BETWEEN 6 AND 10 AND b.SkillCode IS NOT NULL
                AND b.Date < @endDate
            )
            AS Actuals

        ,DATEADD(week,@count,@beginDate) AS DateCounted
        FROM mmapps.dbo.t_EMS_BM_ActualsHistory AS a
        WHERE a.DateCounted = @beginDate
    END

    IF (@count > 0)
    BEGIN
        INSERT INTO @tempTable (BusinessUnitID,BusinessUnitName,SkillCode, Actuals, DateCounted)
        SELECT a.BusinessUnitID, a.BusinessUnitName, a.SkillCode, a.Actuals + 
            (
            SELECT COUNT(*) 
            FROM mmapps.dbo.t_EMS_BM_ChartData AS b 
            WHERE a.BusinessUnitID = b.BusinessUnitID AND a.SkillCode = b.SkillCode 
                AND b.ActivityTypeID BETWEEN 6 AND 10 AND b.SkillCode IS NOT NULL
                AND b.Date >=@beginDate AND b.Date < @endDate) -
            (
            SELECT COUNT(*) 
            FROM mmapps.dbo.t_EMS_BM_ChartData AS b 
            WHERE a.BusinessUnitID = b.BusinessUnitID AND a.SkillCode = b.SkillCode 
                AND b.ActivityTypeID BETWEEN 6 AND 10 AND b.SkillCode IS NOT NULL
                AND b.Date >=@beginDate AND b.Date < @endDate
            )
            AS Actuals

        ,DATEADD(week,@count,@beginDate) AS DateCounted
        FROM @tempTable AS a
        WHERE a.DateCounted = (SELECT MAX(DateCounted) FROM @tempTable)
    END
    SET @count = @count + 1
END

SELECT * FROM @tempTable
ORDER BY DateCounted DESC

经过更多的研究和反复试验,简单地添加 SET NOCOUNT ON; 似乎可以解决我所有的问题。