Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用order by时如何添加列[distanceInKm]?_Sql_Sql Server_Stored Procedures_Procedure - Fatal编程技术网

Sql 使用order by时如何添加列[distanceInKm]?

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

我有一个问题,因为我不知道在我的程序中使用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=@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