我有一个[dbo].[FN_CalcAerialDistance]接受4 parameters并返回其结果的函数。现在 4 个参数来自same table,我不想多次点击数据库来带来相同的数据。我试过了:
SELECT [dbo].[FN_CalcAerialDistance]
(
(select latitude from loc.locations where place_name = 'Delhi'),
(select longitude from loc.locations where place_name = 'Delhi'),
(select latitude from loc.locations where place_name = 'Mumbai'),
(select longitude from loc.locations where place_name = 'Mumbai')
)
Run Code Online (Sandbox Code Playgroud)
有什么方法可以优化代码?我也尝试像数组一样访问表数据类型
我想在表变量中存储 2 个坐标点(纬度、经度)。
我试过了:
declare @coordinates table(latitude1 decimal(12,9),
longitude1 decimal(12,9),
latitude2 decimal(12,9),
longitude2 decimal(12,9))
select latitude,
longitude into @coordinates
from loc.locations
where place_name IN ('Delhi', 'Mumbai')
select @coordinates
Run Code Online (Sandbox Code Playgroud)
它显示错误:
消息 102,级别 15,状态 1,第 2 行 '@coordinates' 附近的语法不正确。
选择查询的结果:
select latitude,
longitude
from loc.locations
where place_name IN ('Delhi', 'Mumbai')
Run Code Online (Sandbox Code Playgroud)
是:
latitude longitude
28.666670000 77.216670000
19.014410000 72.847940000
Run Code Online (Sandbox Code Playgroud)
如何将值存储在表数据类型中?
我运行了查询SELECT @@VERSION并得到了结果:
Microsoft SQL Server 2016 (RTM) - 13.0.1601.5 (X64) 2016 年 4 月 29 日 23:23:58 版权所有 (c) Microsoft Corporation Standard …
我试图将数据存储在一个表变量中@coordinates,该变量包含 2 行和 2 列:纬度和经度。
存储价值:
declare @coordinates table(latitude decimal(12,9), longitude decimal(12,9))
insert into @coordinates select latitude, longitude from loc.locations where place_name IN ('Delhi', 'Mumbai')
Run Code Online (Sandbox Code Playgroud)
查看值:
select * from @coordinates
Run Code Online (Sandbox Code Playgroud)
结果:
latitude longitude
28.666670000 77.216670000
19.014410000 72.847940000
Run Code Online (Sandbox Code Playgroud)
但我想一一访问这些值,例如:@coordinates[1][1] 或 @coordinates.latitude[1]
或任何可能的简单方法?