我正在尝试使用以下代码在 SQL 中创建一个新函数:
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
但我收到以下错误:
消息 102,级别 15,状态 1,过程 PROFITIBLE_CATEGORIES,第 4 行
“@ResultTable”附近的语法不正确。
有人可以帮忙吗?这可能是语法错误,但我是新手。提前致谢。