不交参数怎么写程序?

How to write a procedure without handing over the parameters?

我有一个有四个参数的过程,一个参数是必需的,但另外三个不是必需的。

程序看起来像这样:

CREATE PROCEDURE p_choose_vehicle_byGroup_resultCount_Radiant5
    @IdCustomer int,
    @idGroupVehicle int = null,
    @ResultCount int= null,
    @Radiant int= null 
AS
    DECLARE @start geography
    SET @start = (SELECT location FROM Customer WHERE idCustomer = @idCustomer)

    IF @ResultCount IS NULL AND @idGroupVehicle IS NULL AND @Radiant IS NULL
        SELECT 
            idVehicle, idGroupVehicle, brand, model, maxRange, weight, 
            maxSpeed, nameLocation, @start.STDistance(locationVehicle) / 1000 AS distanceInKm
        FROM
            Vehicle 
        WHERE 
            (@start.STDistance(locationVehicle) / 1000 IS NOT NULL)
        ORDER BY 
            @start.STDistance(locationVehicle) / 1000 ASC
    else if @ResultCount is null and @Radiant is null
    select  idVehicle,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @start.STDistance(locationVehicle)/1000 as distanceInKm
        from Vehicle 
            where  idGroupVehicle= @idGroupVehicle and (@start.STDistance(locationVehicle)/1000  is not null)
            order by @start.STDistance(locationVehicle)/1000 asc
    else if @Radiant is null
    select TOP(@ResultCount) idVehicle,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @start.STDistance(locationVehicle)/1000 as distanceInKm
        from Vehicle 
            where idGroupVehicle= @idGroupVehicle  and  (@start.STDistance(locationVehicle)/1000  is not null)
            order by @start.STDistance(locationVehicle)/1000 asc
    else
    select TOP(@ResultCount) idVehicle,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @start.STDistance(locationVehicle)/1000 as distanceInKm
        from Vehicle 
            where idGroupVehicle= @idGroupVehicle  and  (@start.STDistance(locationVehicle)/1000  <= @Radiant)
            order by @start.STDistance(locationVehicle)/1000 asc
GO

这个过程有效,但我的问题是有什么方法可以改进这个过程,因为当我想要 exec p_choose_vehicle_byGroup_resultCount_Radiant5 1 是好的 1,1 是好的,但是当我尝试选择 idCustomer 和 resultCount 时,我必须这样做1,null,1 但我不想写 null 并选择此选项有什么方法可以改进此过程?

除非我不理解,否则你只需要像

那样调用你的程序
exec p_choose_vehicle_byGroup_resultCount_Radiant5 @IdCustomer=1, @resultCount=1