多语句标量到多语句TVF

时间:2016-07-17 00:32:42

标签: sql-server variables user-defined-functions table-valued-parameters

我对这几行代码有疑问,特别是如何使用StartingCost和EndingCost值填充@workTable:

DECLARE @workTable TABLE
                (
                  ProductId INT ,
                  StartingCost MONEY ,
                  EndingCost MONEY
                ) ;

完整查询如下所示。

IF OBJECT_ID(N'Production.ms_tvf_ProductCostDifference',N'TF' ) IS NOT NULL

--SELECT * FROM sys.objects WHERE name LIKE 'm%'
    DROP FUNCTION Production.ms_tvf_ProductCostDifference ;
GO

CREATE FUNCTION Production.ms_tvf_ProductCostDifference
    (
      @StartDate DATETIME ,
      @EndDate DATETIME 
    )
RETURNS @retCostDifference TABLE
    (
      ProductId INT ,
      CostDifference MONEY
    )
AS
    BEGIN
        DECLARE @workTable TABLE
            (
              ProductId INT ,
              StartingCost MONEY ,
              EndingCost MONEY
            ) ;

        INSERT  INTO @retCostDifference
                ( ProductId ,
                  CostDifference
                )
                SELECT  ProductID ,
                        StandardCost
                        FROM    ( SELECT    pch.ProductID ,
                                    pch.StandardCost ,
                                    ROW_NUMBER() OVER
                                     ( PARTITION BY ProductID
                                       ORDER BY StartDate DESC ) AS rn
                          FROM      Production.ProductCostHistory AS pch
                          WHERE     EndDate BETWEEN
                                                 @StartDate AND @EndDate
                        ) AS x
                WHERE   x.rn = 1 ;

        UPDATE  @retCostDifference
        SET     CostDifference = CostDifference - StandardCost
        FROM    @retCostDifference cd
                JOIN ( SELECT   ProductID ,
                                StandardCost
                       FROM     ( SELECT    pch.ProductID ,
                                            pch.StandardCost ,
                                            ROW_NUMBER() OVER
                                             ( PARTITION BY ProductID
                                               ORDER BY StartDate ASC )
                                               AS rn
                                  FROM      Production.ProductCostHistory 
                                                AS pch
                                  WHERE     EndDate BETWEEN
                                                 @StartDate AND @EndDate
                                ) AS x 
                       WHERE    x.rn = 1
                     ) AS y ON cd.ProductId = y.ProductID ;

        RETURN ; select top 20 * from Production.ProductCostHistory 
          END
Go

/ ********************************************** ********************************** 上面的代码代表

清单17:多语句TVF

此TVF,而不是从数据库中检索单行并进行计算 价格差异,从数据库中拉回所有行并计算 一次性所有行的价格差异。 ************************************************** ********* /

SELECT  p.ProductID ,
        p.Name ,
        p.ProductNumber ,
        pcd.CostDifference
FROM    Production.Product AS p
        INNER JOIN Production.ms_tvf_ProductCostDifference
                               ('2001-01-01', GETDATE()) AS pcd
                                 ON p.ProductID = pcd.ProductID ;

1 个答案:

答案 0 :(得分:0)

在您的代码中,简单地声明了下面的块,它从未使用过。

DECLARE @workTable TABLE
                (
                  ProductId INT ,
                  StartingCost MONEY ,
                  EndingCost MONEY
                ) ;

由于@retCostDifference是此函数的返回表,所有事务(INSERTUPDATE)仅发生在@retCostDifference表中。

函数中没有使用@workTable