程序只插入一条记录

Procedure inserts only one record

我有这个存储过程:

CREATE PROCEDURE [dbo].[TVP_OfferPrice] @OfferPriceTVP TVP_OfferPrice READONLY
AS 
BEGIN
    DECLARE @OfferId INT;
    DECLARE @CountryId INT ;
    DECLARE @VatRateId INT ;
    DECLARE @SalePrice DECIMAL(16, 4) ;
    DECLARE @SaleFromDate DATETIME;
    DECLARE @SaleToDate DATETIME;
    DECLARE @DefaultPrice DECIMAL(16, 4);
    DECLARE @Price DECIMAL(16,4);

    SELECT  
        @OfferId = a.OfferId, @CountryId = a.CountryId, @VatRateId = a.VatRateId,
        @SalePrice = a.SalePrice, @SaleFromDate = a.SaleFromDate, @SaleToDate = a.SaleToDate, 
        @DefaultPrice =a.DefaultPrice 
    FROM 
        @OfferPriceTVP a;

    SET @Price = (SELECT TOP 1 pp.Price  
                  FROM [dbo].[Promotion] p 
                  INNER JOIN [dbo].[PromotionProduct] pp ON pp.ProductId = p.Id
                  INNER JOIN [dbo].[Offer] do ON do.ProductId = pp.ProductId AND do.Id = @OfferId 
                  INNER JOIN [dbo].[PromotionAssignment] pda ON pda.PromotionId = p.Id AND pda.Id = do.Id 
                  WHERE p.CountryId = @CountryId
                    AND GETUTCDATE() >= p.ValidFrom AND GETUTCDATE() < p.ValidTo 
                  ORDER BY p.ValidFrom DESC, pp.Price)

    IF(@Price IS NULL AND @SalePrice IS NOT NULL AND GETUTCDATE() >= @SaleFromDate AND GETUTCDATE() < @SaleFromDate)
        SET @Price = @SalePrice

    IF @Price IS NULL
        SET @Price = @DefaultPrice

    IF NOT EXISTS (SELECT * FROM [dbo].[OfferPrice] dop  WHERE dop.OfferId = @OfferId AND dop.CountryId = @CountryId)
        INSERT INTO [dbo].[OfferPrice](OfferId, CountryId, VatRateId, Price, DefaultPrice, SalePrice, SaleFromDate, SaleToDate)
           SELECT 
              @OfferId, @CountryId, @VatRateId, @Price, @DefaultPrice, 
              @SalePrice, @SaleFromDate, @SaleToDate 
    ELSE
        UPDATE b 
        SET b.VatRateId = @VatRateId, @Price = @Price, b.DefaultPrice = @DefaultPrice, 
            b.SalePrice = @SalePrice, b.SaleFromDate = @SaleFromDate, b.SaleToDate = @SaleToDate 
        FROM
            [dbo].OfferPrice b 
        WHERE 
            b.OfferId = @OfferId AND b.CountryId = @CountryId;
END

当我尝试使用某些值执行它时,例如:

DECLARE @OfferPriceTVP AS [dbo].[TVP_DealerOfferPrice]

INSERT INTO @OfferPriceTVP (DealerOfferId, CountryId, VatRateId, DefaultGrossPrice, SaleGrossPrice, SaleFromDate, SaleToDate)
VALUES (10006805, 1, 1, 1, 1, 2, NULL),
(10006806, 1, 1, 2, 1, NULL, NULL),
(10006807, 1, 1, 3, 1, NULL, NULL),
(10006808, 1, 1, 4, 1, NULL, NULL),
(10006809, 1, 1, 5, 1, NULL, NULL),
(10006810, 1, 1, 6, 1, NULL, NULL);

EXEC [dbo].[TVP_DealerOfferPrice] @OfferPriceTVP;
GO

SQL 服务器显示只有 1 行受到影响,实际上最后一个值只进入我的 table。知道为什么吗?

基本上,由于您的变量不能同时保存多个值,因此使用以下语句:

SELECT @OfferId = a.OfferId
      ,@CountryId = a.CountryId
      ,@VatRateId = a.VatRateId
      ,@SalePrice = a.SalePrice
      ,@SaleFromDate = a.SaleFromDate
      ,@SaleToDate = a.SaleToDate
      ,@DefaultPrice = a.DefaultPrice
FROM @OfferPriceTVP a;

您只保留了一条输入记录 table。

我猜您正试图将输入 table 与报价 table 合并。所以,你最好使用 MERGE 语句。这是一个例子:

MERGE OfferPrice AS TARGET
USING (SELECT VatRateId
             ,CASE WHEN Price IS NULL AND SalePrice IS NOT NULL AND GETUTCDATE() >= SaleFromDate AND GETUTCDATE() < SaleFromDate THEN SalePrice ELSE DefaultPrice END AS Price
             -- And so on and so forth
       FROM @OfferPriceTVP) AS SOURCE
    ON TARGET.OfferId = SOURCE.OfferId
WHEN MATCHED THEN
    UPDATE SET VatRateId = SOURCE.VatRateId
              ,Price     = SOURCE.Price
              -- And so on and so forth
WHEN NOT MATCHED THEN
    INSERT (OfferId, CountryId) -- And so on and so forth
    VALUES (SOURCE.OfferId, SOURCE.CountryId) -- And so on and so forth

更多信息在这里:

MERGE (Transact-SQL)

CASE (Transact-SQL)