I am trying to use CASE statement during the insert in sql Stored procedure like this:
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
Problem is that CASE is always skipping to the ELSE even if previous statements are true. What am I doing wrong?
EDIT:
This is @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
);
And here the insert I was trying to do now (even though there are no dates it should set price to default right?):
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