Sql 使用order by时如何添加列[distanceInKm]?
我有一个问题,因为我不知道在我的程序中使用order by时如何添加kolumn,客户和车辆之间的排序距离 我的程序是这样的:Sql 使用order by时如何添加列[distanceInKm]?,sql,sql-server,stored-procedures,procedure,Sql,Sql Server,Stored Procedures,Procedure,我有一个问题,因为我不知道在我的程序中使用order by时如何添加kolumn,客户和车辆之间的排序距离 我的程序是这样的: create procedure [dbo].[p_top5_v2_type2222] @IdCustomer int, @idGroupVehicle int as declare @start geography SET @start = (select location from Customer where idCustomer=@idCust
create procedure [dbo].[p_top5_v2_type2222]
@IdCustomer int,
@idGroupVehicle int as
declare @start geography
SET @start = (select location from Customer where idCustomer=@idCustomer )
select TOP 5 idVehicle,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation
from Vehicle where idGroupVehicle= @idGroupVehicle and (@start.STDistance(locationVehicle)/1000 is not null)
order by @start.STDistance(locationVehicle)/1000 asc
GO
我的结果是
现在我想添加列[distanceInKm]
看看它们是否真的分类正确
我试着
但我有错误
Msg 102, Level 15, State 1, Procedure p_top5_v2_type_distance, Line 7 [Batch Start Line 0]
Incorrect syntax near 'distanceInKm'.
有人能解释一下我如何添加列吗?所选列进入
选择
,而不是排序依据
。然后,您可以按的顺序使用它:
select TOP 5 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 distanceInKm asc
好的,我检查了这个,但是[distanceInKm]附近有错误错误语法,我删除了[distanceInKm],现在是正确的,谢谢mate@Konrad . . . 真奇怪。我不认为我修改了
where
子句——显然不是我的本意。
select TOP 5 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 distanceInKm asc