我创建了一个名为fnDiscountPrice的函数,它接受一个参数(ItemID)并计算该项的折扣价:
CREATE FUNCTION fnDiscountPrice (@ItemID INT)
RETURNS MONEY
AS
BEGIN
RETURN (SELECT (ItemPrice - DiscountAmount)
FROM OrderItems
WHERE ItemID = @ItemID
)
END;
现在我正在尝试创建另一个名为fnItemTotal的函数,该函数接受一个参数(ItemID)并计算Total Item Price(这是DiscountPrice * Quantity),所以我想在这个函数中使用我之前的函数。
这就是我的想法:
CREATE FUNCTION fnItemTotal (@ItemID INT)
RETURNS INT
AS
BEGIN
RETURN (SELECT (dbo.fnDiscountPrice(@ItemID) * Quantity) AS 'TotalAmount'
FROM OrderItems
)
END;
它在我的数据库中成功创建了此功能。但我无法称呼它。当我尝试使用:
调用我的函数时SELECT dbo.fnItemTotal(3);
它返回此MSG:
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
如果我将鼠标悬停在代码中突出显示的错误上,它会给我提供工具提示:
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fnItemTotal", or the name is ambiguous.
我在这里做错了什么?