1

我有一个这样的存储过程:

ALTER PROCEDURE [dbo].[Driverperformance] 
    @Ecode NVARCHAR(50), 
    @startdate DATETIME, 
    @enddate DATETIME
AS
BEGIN
    DECLARE @date1 DATETIME = CONVERT(DATETIME, @startdate + ' 00:01:00.000', 120);
    DECLARE @date2 DATETIME = CONVERT(DATETIME, @enddate + ' 23:23:59.000', 120);
    SELECT  e.Ecode
        ,   CAST(q.dtime AS DATE) AS Date
        ,   e.Ename
        ,   COUNT(q.Ecode) CntEcode
        ,   (
                SELECT COUNT(*)
                FROM Transaction_tbl
                WHERE dtime >= '' + @date1 + ''
                    AND dtime <= '' + @date2 + ''
                    AND DelEcode = @Ecode
                GROUP BY CAST(dtime AS DATE)
            )
    FROM EmployeeMaster_tbl e
    JOIN Transaction_tbl q ON e.Ecode = q.Ecode
    WHERE q.Ecode = @Ecode
        AND dtime >= '' + @date1 + ''
        AND dtime <= '' + @date2 + ''
    GROUP BY
        e.Ecode
    ,   e.Ename
    ,   CAST(q.dtime AS DATE)
    ORDER BY CAST(q.dtime AS DATE)
END

执行此查询时出现如下错误:

子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。

4

1 回答 1

0

在您的查询中,您有:

(
    SELECT COUNT(*)
    FROM Transaction_tbl
    WHERE dtime >= '' + @date1 + ''
        AND dtime <= '' + @date2 + ''
        AND DelEcode = @Ecode
    GROUP BY CAST(dtime AS DATE)
)

作为一列。上面因为 GROUP BY 将为您选择的每一行返回多行。这在 SQL 中是不可能的。

于 2013-09-04T11:52:56.243 回答