存储过程中的 Case 语句总是跳到 ELSE
Case statement always skipping to ELSE in stored procedure
我正在尝试在 sql 存储过程中插入时使用 CASE 语句,如下所示:
INSERT INTO [dbo].[OfferPrice]
(OfferId,Price,DefaultPrice,SalePrice,
SaleFromDate,SaleToDate)
SELECT tvp.OfferId AS OfferId,
CASE
WHEN @Price IS NOT NULL THEN @Price
WHEN @Price IS NULL
AND tvp.SalePrice IS NOT NULL
AND Getutcdate() >= tvp.SaleFromDate
AND Getutcdate() < tvp.SaleToDate THEN tvp.SalePrice
WHEN @Price IS NULL
AND tvp.SalePrice IS NULL THEN tvp.DefaultPrice
ELSE 0
END AS Price,
tvp.DefaultPrice AS DefaultPrice,
tvp.SalePrice AS SalePrice,
tvp.SaleFromDate AS SaleFromDate,
tvp.SaleToDate AS SaleToDate
FROM @OfferPriceTVP tvp
LEFT JOIN [dbo].OfferPrice dop
ON dop.OfferId = tvp.OfferId
WHERE dop.OfferId IS NULL
问题是 CASE 总是跳到 ELSE,即使前面的陈述是正确的。我做错了什么?
编辑:
我是@OfferPriceTVP:
CREATE TYPE [dbo].[TVP_OfferPrice] AS TABLE
(
OfferId INT NOT NULL PRIMARY KEY, CountryId INT NOT NULL, VatRateId INT, DefaultPrice decimal(16, 4), SalePrice decimal(16, 4),
SaleFromDate datetime, SaleToDate datetime
);
这里是我现在尝试做的插入(即使没有日期它应该将价格设置为默认价格对吗?):
DECLARE @OfferPriceTVP AS [dbo].[TVP_OfferPrice]
INSERT INTO @OfferPriceTVP (OfferId,CountryId,VatRateId,DefaultPrice,SalePrice,SaleFromDate,SaleToDate)
VALUES (10006805,2,1,1,1,NULL,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,2,NULL,NULL);
EXEC [dbo].[TVP_OfferPrice] @OfferPriceTVP;
GO
与 NULL 的比较总是错误的。您的 table 不包含销售日期。销售期检查
Getutcdate() >= tvp.SaleFromDate AND Getutcdate() < tvp.SaleToDate
总是会失败。
这意味着如果 @Price
为 NULL 而 SalePrice
不是,则只有 ELSE
语句有效。
我认为,如果没有销售日期,您的意图可能是采用销售价格(如果有)或默认价格。
如果是这种情况,则只需省略第三个 WHEN
中的 SalePrice IS NULL
检查并使用 COALESCE
:
CASE
WHEN @Price IS NOT NULL THEN @Price
WHEN @Price IS NULL
AND tvp.SalePrice IS NOT NULL
AND Getutcdate() >= tvp.SaleFromDate
AND Getutcdate() < tvp.SaleToDate THEN tvp.SalePrice
WHEN @Price IS NULL
THEN COALESCE(tvp.SalePrice,tvp.DefaultPrice)
ELSE 0
END AS Price
我正在尝试在 sql 存储过程中插入时使用 CASE 语句,如下所示:
INSERT INTO [dbo].[OfferPrice]
(OfferId,Price,DefaultPrice,SalePrice,
SaleFromDate,SaleToDate)
SELECT tvp.OfferId AS OfferId,
CASE
WHEN @Price IS NOT NULL THEN @Price
WHEN @Price IS NULL
AND tvp.SalePrice IS NOT NULL
AND Getutcdate() >= tvp.SaleFromDate
AND Getutcdate() < tvp.SaleToDate THEN tvp.SalePrice
WHEN @Price IS NULL
AND tvp.SalePrice IS NULL THEN tvp.DefaultPrice
ELSE 0
END AS Price,
tvp.DefaultPrice AS DefaultPrice,
tvp.SalePrice AS SalePrice,
tvp.SaleFromDate AS SaleFromDate,
tvp.SaleToDate AS SaleToDate
FROM @OfferPriceTVP tvp
LEFT JOIN [dbo].OfferPrice dop
ON dop.OfferId = tvp.OfferId
WHERE dop.OfferId IS NULL
问题是 CASE 总是跳到 ELSE,即使前面的陈述是正确的。我做错了什么?
编辑:
我是@OfferPriceTVP:
CREATE TYPE [dbo].[TVP_OfferPrice] AS TABLE
(
OfferId INT NOT NULL PRIMARY KEY, CountryId INT NOT NULL, VatRateId INT, DefaultPrice decimal(16, 4), SalePrice decimal(16, 4),
SaleFromDate datetime, SaleToDate datetime
);
这里是我现在尝试做的插入(即使没有日期它应该将价格设置为默认价格对吗?):
DECLARE @OfferPriceTVP AS [dbo].[TVP_OfferPrice]
INSERT INTO @OfferPriceTVP (OfferId,CountryId,VatRateId,DefaultPrice,SalePrice,SaleFromDate,SaleToDate)
VALUES (10006805,2,1,1,1,NULL,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,2,NULL,NULL);
EXEC [dbo].[TVP_OfferPrice] @OfferPriceTVP;
GO
与 NULL 的比较总是错误的。您的 table 不包含销售日期。销售期检查
Getutcdate() >= tvp.SaleFromDate AND Getutcdate() < tvp.SaleToDate
总是会失败。
这意味着如果 @Price
为 NULL 而 SalePrice
不是,则只有 ELSE
语句有效。
我认为,如果没有销售日期,您的意图可能是采用销售价格(如果有)或默认价格。
如果是这种情况,则只需省略第三个 WHEN
中的 SalePrice IS NULL
检查并使用 COALESCE
:
CASE
WHEN @Price IS NOT NULL THEN @Price
WHEN @Price IS NULL
AND tvp.SalePrice IS NOT NULL
AND Getutcdate() >= tvp.SaleFromDate
AND Getutcdate() < tvp.SaleToDate THEN tvp.SalePrice
WHEN @Price IS NULL
THEN COALESCE(tvp.SalePrice,tvp.DefaultPrice)
ELSE 0
END AS Price