【发布时间】: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