SQL Server存储过程查询返回多列

时间:2019-04-15 09:59:10

标签: sql-server tsql geospatial

以下查询返回4列,但是如果我尝试从存储过程返回相同的列,则会得到

  

如果未使用EXISTS引入子查询,则只能在选择列表中指定一个表达式

如果我想获取以逗号分隔的字符串返回的值,该怎么写?

工作查询

Declare @g geography = 'POINT(-1.2846387 52.686091)'  

Select top 1 
    Round(Geocolumn.STDistance(@g)/1000, 2) as DistanceInKlms,
    Registration, location, dateoffix  
from 
    Positions  
where
    Geocolumn.STDistance(@g) is not null
    and Registration = 'DX17AAF'
order by
    Geocolumn.STDistance(@g);

存储过程因多列而中断:

select @return =    (Select Top(1)
          Round(GeographyPositon.STDistance(@g)/1000, 2) as DistanceInKlms,
          Registration, [location], dateoffix  
from Positions 
WHERE   Registration = @Registration
ORDER BY GeographyPositon.STDistance(@g))

2 个答案:

答案 0 :(得分:0)

您需要那个吗?

select @return =    (Select Top(1)
          Round(GeographyPositon.STDistance(@g)/1000, 2)+' , '
          Registration+' , '+ [location] +' , '+ dateoffix  
from Positions 
WHERE   Registration = @Registration
ORDER BY GeographyPositon.STDistance(@g)

答案 1 :(得分:0)

尝试以下操作以在单个字符串中获取所有4个列值。

select @return =    ( Select Top(1)
          CAST(Round(GeographyPositon.STDistance(@g)/1000, 2) AS VARCHAR)+', '
          CAST(Registration AS VARCHAR)+', '+ CAST([location] AS VARCHAR) +', '+ CAST(dateoffix AS VARCHAR)
from Positions 
WHERE   Registration = @Registration
ORDER BY GeographyPositon.STDistance(@g) )