运行sql server后会显示此错误
子查询返回的值超过1。这是不允许的 子查询跟随=,!=,<,< =,>,> =或当子查询用作 表达。
以下是我的存储过程的脚本
SELECT
(SELECT SUM(GrossSales)
FROM cssale
WHERE EndDate BETWEEN @StartDate AND @EndDate
AND CashierID = @Employee) AS [Total Sales],
(SELECT SUM(GrossSales)
FROM CSSale
WHERE RefundStoreDate BETWEEN @StartDate AND @EndDate
AND CashierID = @Employee) AS [Refunds],
(SELECT SUM(GrossSales)
FROM CSSale
WHERE DateVoided BETWEEN @StartDate AND @EndDate
AND CashierID = @Employee) AS [Voided Items],
( SELECT sum(isnull(C.Amount,0)) AS [Discount Amount]
FROM CSSaleItem B
LEFT JOIN CSSaleItemDiscount C ON B.CSSaleItemID = C.CSSaleItemID
LEFT JOIN CSSale A ON A.CSSaleID = B.CSSaleID
LEFT JOIN CSSaleItemTax D ON B.CSSaleItemID = D.CSSaleItemID
LEFT JOIN
(SELECT CSSaleItemID,
Amount,
Tax,
[Type]
FROM CSSaleItemTax
WHERE [Type] = 0) AS vsa ON vsa.CSSaleItemID = B.CSSaleItemID
WHERE A.EndDate BETWEEN @StartDate AND @EndDate
AND A.CashierID = @Employee) AS [Discount],
(SELECT CashierId
FROM cssale
WHERE EndDate BETWEEN @StartDate AND @EndDate
AND CashierID = @Employee)AS [Cashier]
答案 0 :(得分:1)
子查询只能返回一个值。在你的代码下面,行可能是罪魁祸首..
(SELECT CashierId
FROM cssale
WHERE EndDate BETWEEN @StartDate AND @EndDate
AND CashierID = @Employee)AS [Cashier]
如果要返回所有返回的行或使用top 1限制为仅一行,则可以使用交叉申请/外部申请
答案 1 :(得分:0)
您的问题是(如错误所述),您的子查询返回多个值。就像你试图将一个表的完整列写入一个单元格一样。
尝试使用JOINS
而不是此子查询来解决此查询。
答案 2 :(得分:0)
如果员工在给定期间内进行多次销售,则下面的代码将返回多行。
(SELECT CashierId
FROM cssale
WHERE EndDate BETWEEN @StartDate AND @EndDate
AND CashierID = @Employee)AS [Cashier]
而不是使用跨国表(cssales);尝试使用具有Employee到Cashier映射的主/域表。这样每次都会产生独特的行。