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 に答える