0

我正在尝试使用以下代码在 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”附近的语法不正确。

有人可以帮忙吗?这可能是语法错误,但我是新手。提前致谢。

4

2 回答 2

2

一种更有效的方法是内联表值函数:

CREATE FUNCTION dbo.PROFITIBLE_CATEGORIES
(
  @startDate date, 
  @endDate date
)
RETURNS TABLE
WITH SCHEMABINDING
AS
    RETURN 
    (
       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
    );
GO
于 2012-06-21T12:32:53.580 回答
1

如果您要向表中插入行,那么试试这个

   CREATE FUNCTION PROFITIBLE_CATEGORIES (@startDate date, @endDate date)
   RETURNS @ResultTable TABLE (Name varchar(20), AMOUNT INT)
   AS BEGIN
    INSERT INTO @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
于 2012-06-21T12:26:58.293 回答