尝试创建新函数时,Sql代码无法正常工作

时间:2012-06-21 12:22:55

标签: sql sql-server tsql

我正在尝试使用以下代码在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

但我收到以下错误:

  

Msg 102,Level 15,State 1,Procedure PROFITIBLE_CATEGORIES,Line 4   
'@ResultTable'附近的语法不正确。

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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