I’m trying to create a new function in SQL with the following code:
CREATE FUNCTION PROFITIBLE_CATEGORIES (@startDate date, @endDate date)
RETURNS @ResultTable TABLE (Name varchar(20), AMOUNT INT)
AS BEGIN
@ResultTable = SELECT TOP 5 NAME, COUNT(*) AS AMOUNT
FROM dbo.PURCHASES P JOIN
(SELECT PInC.NAME, PInC.[Walmart number],
PInSP.[Purchase Id] AS PInSP_PURCHASE_ID,
DPP.[Purchase Id] AS DPP_PURCHASE_ID
FROM dbo.PRODUCTS_IN_CATEGORIES PInC LEFT JOIN
dbo.Products_in_self_pick_up_purchases PInSP ON
PInC.[Walmart number] = PInSP.[Walmart number] LEFT JOIN
dbo.Delivered_products_purchases DPP ON
DPP.[Walmart number] = PInC.[Walmart number]) X
ON (P.[Purchase Id] = X.DPP_PURCHASE_ID)
OR (P.[Purchase Id] = X.PInSP_PURCHASE_ID)
WHERE [Date] < @endDate AND [Date] > @startDate
GROUP BY NAME
ORDER BY AMOUNT
RETURN
END
but I’m getting the following error:
Msg 102, Level 15, State 1, Procedure PROFITIBLE_CATEGORIES, Line 4
Incorrect syntax near ‘@ResultTable’.
anyone can help?
It’s probably a syntax error, but I’m new to this.
thanks in advance.
A slightly more efficient approach would be an inline table-valued function: