我有一个标量UDF函数,在调用时应返回一行。它实际上为表中的每一行返回相同的值27次。我可以很容易地添加一个' Distinct'或者' Top 1'当我调用该函数但我不应该这样做。
这是功能代码:
CREATE FUNCTION dbo.TotalIncome(@name AS VARCHAR(10), @weekday AS VARCHAR(10))
RETURNS INT
AS
BEGIN
DECLARE @total int;
SELECT @total = SUM(IncomeAmount)
FROM DailyIncome
WHERE VendorName = @name
AND EachDay = @weekday;
IF (@total IS NULL)
SET @total = 0;
RETURN @total;
END;
这是我调用函数的代码:
SELECT dbo.TotalIncome('SPIKE', 'FRI') AS TotalIncome
FROM dbo.DailyIncome
它应该返回一行,但它返回27.
答案 0 :(得分:2)
这是因为dbo.DailyIncome
中有27行。每行调用一次UDF。也许你想要这样的东西:
SELECT dbo.TotalIncome('SPIKE', 'FRI') AS TotalIncome