I have created a Scalar UDF as follows:
DROP FUNCTION IF EXISTS fnDiscountPrice
GO
CREATE FUNCTION fnDiscountPrice
(@Item_ID int)
RETURNS MONEY
BEGIN
DECLARE @Discount_Price money
SELECT @Discount_Price = ItemPrice - DiscountAmount
FROM OrderItems
WHERE ItemID = @Item_ID
RETURN @Discount_Price
END
GO
I am trying to use this function to create another function here:
DROP FUNCTION IF EXISTS fnItemTotal
GO
CREATE FUNCTION fnItemTotal
(@Item_ID int)
RETURNS MONEY
BEGIN
DECLARE @Item_Total MONEY
SELECT @Item_Total = (dbo.fnDiscountPrice(ItemID) * Quantity)
FROM OrderItems
WHERE ItemID = @Item_ID
RETURN @Item_Total
END
GO
SELECT ItemID, ItemPrice, DiscountAmount, Quantity,
dbo.fnDiscountPrice(ItemId) AS DiscountPrice, dbo.fnItemTotal(ItemID) AS ItemTotal
FROM OrderItems
My problem is that the fnDiscountPrice function is giving me an error "Cannot find either column "dbo" or the user-defined function or aggregate 'dbo.fnDiscountPrice', or the name is ambiguous."
I am not sure what the issue is here. The function exists in the programmability folder in database.
Any ideas?