我有一个存储过程。在where where条件中,我想选择not null值,如果有,否则从表中选择null值。任何人请帮助我..
ALTER PROCEDURE SearchPromotionBYPromotionANDVehicleID
@PromotionCode VARCHAR(100)
,@TypeId INT
,@LocationId INT
,@ClientId INT
AS
BEGIN
SET NOCOUNT ON;
SELECT top 1 tp.PromotionID
,tp.PromotionCode
,tp.ClientID
,tp.StartDate
,tp.EndDate
,tp.VehicleTypeId
,tv.VehicleType
,tp.LocationId
FROM tblstudent tp
LEFT OUTER JOIN tblType tv ON tp.TypeId = tv.TypeId
WHERE tp.PromotionCode = @PromotionCode
AND
( ( @LocationId IS NOT NULL AND tp.LocationId = @LocationId ) OR (tp.LocationId IS NULL) OR (@LocationId IS NULL) or (tp.LocationId = 0 ) )
在此考虑2个记录(r1和r2)具有相同的促销代码,一个记录具有所有位置(即位置ID 0),另一个记录具有位置ID。当参数@locationId有一个值(考虑值等于记录r2的值)时,它必须返回记录2.否则必须返回r1。
答案 0 :(得分:0)
WHERE tp.PromotionCode = @PromotionCode
AND (( @LocationId IS NOT NULL AND tp.LocationId = @LocationId ) OR tp.LocationId IS NULL)
此条件选择 @LocationId 具有非空且非0值的值,并且该值需要与 tp.LocationId 匹配,否则返回其中的值 tp.LocationId 为空。 这是你想要的结果吗?如果你想要不同,请告诉我