0

我有这个存储过程

ALTER PROCEDURE [dbo].[usp_mySP]
    -- Add the parameters for the stored procedure here
    -- For inventory table check.
    @DealerID       VARCHAR(50),
    @Certified      VARCHAR(50) = NULL,
    -- For fuel value.
    @CityFeatureValue   VARCHAR(50),
    @HwyFeatureValue    VARCHAR(50)

AS
BEGIN
        BEGIN
        SELECT  InventoryID, 
                VIN
        FROM    Inventory
        WHERE   DealerID = @DealerID
                AND Deleted     =   'False'
                AND (IsPending  =   '0'         OR IsPending    IS NULL) 
                --AND (Certified    =   @Certified  OR @Certified   IS NULL)

                AND VIN IN
                        (
                            SELECT      VIN 
                            FROM        FuelPerformance
                            WHERE       (
                                            FeatureTitle = 'Fuel Economy (City)' 
                                            AND FeatureValue = @CityFeatureValue
                                        )
                                        OR 
                                        (
                                            FeatureTitle = 'Fuel Economy (Hwy)' 
                                            AND FeatureValue = @HwyFeatureValue
                                        )
                            GROUP BY    VIN
                            HAVING      COUNT(VIN) > 1
                        )
    END
END

我这样称呼它:

EXEC usp_ListOfVehiclesOnFuelCondition_ForSingleDealer
   '09f5245d' , '', '18', '28'

当我评论该行时

 AND (Certified = @Certified OR @Certified IS NULL)

它给出了结果,但是当这条线在那里时,结果是空白的。

有什么建议我做错了吗?

4

1 回答 1

5

您正在传入一个空字符串 ( '')。这与NULL.

将参数作为命名参数(不带@Certified)传入,或测试空字符串,或传入NULL.

命名参数:

EXEC usp_ListOfVehiclesOnFuelCondition_ForSingleDealer @DealerId = '09f5245d',
                                                       @CityFeatureValue = '18', 
                                                       @HwyFeatureValue = '28'

通过一个NULL

EXEC usp_ListOfVehiclesOnFuelCondition_ForSingleDealer '09f5245d', 
                                                       NULL, 
                                                       '18', 
                                                       '28'

检查''

AND (Certified = @Certified OR @Certified IS NULL OR @Certified = '')
于 2012-12-26T13:56:09.553 回答