【问题标题】:How to fix procedure with many parameters如何修复具有许多参数的程序
【发布时间】:2021-03-29 16:12:48
【问题描述】:

我有一个包含四个参数的过程,但我的组合之一是错误的 当我尝试执行 p_shearch_vehicle @idCustomer=1,@ResultCount=2 时没有得到结果。

我的程序如下所示:

create procedure    p_shearch_vehicle3
@IdCustomer int,
 @idGroupVehicle int = null,
 @ResultCount int= null,
 @Radiant int= null 
 as
 begin
 if @IdCustomer is null
    begin
        print 'The argument cannot be null'
        return 
    end
 declare @start geography
 SET @start = (select location from Customer where idCustomer=@idCustomer )
 ---@Result null group null radiant null
    if @ResultCount is null and @idGroupVehicle is null and @Radiant is null
        begin
    select top 10 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
        end
             ---@Result null  radiant null
            else if @ResultCount is null and @Radiant is null
            begin
    select  top 10 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
            end
             ---@Result null  
            else if @Radiant is null
            begin
    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
            end
             ---@@idGroupVehicle  null @Radiant is null
            else if  @idGroupVehicle is null and @Radiant is null
            begin
    select TOP(@ResultCount) 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
            end
            ---@idGroupVehicle is null and @ResultCount is null
            else if  @idGroupVehicle is null and @ResultCount is null
            begin
    select top 10 idVehicle,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @start.STDistance(locationVehicle)/1000 as distanceInKm
        from Vehicle 
            where  (@start.STDistance(locationVehicle)/1000   <= @Radiant)
            order by @start.STDistance(locationVehicle)/1000 asc
            end
        --- @idGroupVehicle is null 
            else if  @idGroupVehicle is null 
            begin
    select TOP(@ResultCount) idVehicle,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @start.STDistance(locationVehicle)/1000 as distanceInKm
        from Vehicle 
            where  (@start.STDistance(locationVehicle)/1000   <= @Radiant)
            order by @start.STDistance(locationVehicle)/1000 asc
            end
            --- all options
    else
    begin
    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
            end
 end
GO

我的错误是@idGroupVehicle = NULL AND @Radiant = NULL;使用这种组合,我没有得到任何结果,但是当我这样检查时:

CREATE PROCEDURE [dbo].[p_search_vehicle]
    @IdCustomer int,
    @ResultCount int = NULL
AS
BEGIN
    IF @IdCustomer IS NULL
    BEGIN
        PRINT 'The argument cannot be null'
        RETURN 
    END

    DECLARE @start geography
    SET @start = (select location from Customer where idCustomer=@idCustomer )
select TOP(@ResultCount) 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
            end
            go

我的结果是:

有人可以解释为什么我可以在我的程序中得到这个结果吗?

【问题讨论】:

  • 四个参数是“多”?
  • 能否尝试在每个 IF 语句中加上 BEGIN...END 以清楚地看到 IF 语句的范围?
  • @Blindy 对你来说是很多机器人

标签: sql-server stored-procedures procedure


【解决方案1】:

我认为您希望首先遇到这种情况:

else if  @idGroupVehicle is null and @Radiant is null

但实际上,这个条件首先受到打击:

else if @Radiant is null

所以在这种情况下,当@Radiant 为 NULL 时,触发的 SELECT 语句就是这个:

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

我敢打赌,如果你自己运行那个 select 语句,它也不会返回数据。

在您的决策结构中,'else if @Radiant is NULL' 检查位于'else if @idGroupVehicle 为 NULL 且 @Radiant 为 NULL' 检查之前。

这意味着“else if @Radiant is NULL”检查中的 SELECT 语句将运行,然后不会运行其他 SELECT 语句。

如果您希望触发多个 SELECT 语句,则应将“ELSE IF”语句更改为“IF”语句。

或者,您可以重新排列您的代码,让“else if @idGroupVehicle is NULL and @Radiant is NULL”检查位于“else if @Radiant is NULL”检查之前。

希望对您有所帮助!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
  • 2017-02-05
  • 2021-11-19
  • 1970-01-01
相关资源
最近更新 更多