调用用户定义的函数

时间:2019-06-23 01:34:54

标签: sql

我有一个名为get_distance(point1,point2)的函数,该函数计算两点之间的距离。我想计算表格(几何)的列顶行中的点(点)与已经定义的变量(point_variable)之间的距离。我正在运行以下无法终止的查询:

-- Assume point_variable has already been defined as a varchar and 
-- has a value
-- Assume that there is a column (points) in my table 
-- (geometry) with only points stored as varchar
-- Also, assume that there is a scalar function (get_distance) that 
-- takes as input two points (as 2 varchar parameters)and computes 
-- and returns the distance as an int
SELECT get_distance(point_variable, (SELECT TOP 1 points from 
geometry))

我正在运行前面的代码,查询永远不会终止。我的目标是获取点列顶部行中的条目与变量point_variable之间的距离。我应该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试将查询结果声明为变量,然后将其传递给函数:

    DECLARE @POINTS_VAR VARCHAR(MAX)
    SET @POINTS_VAR = (SELECT TOP 1 points from geometry)
    SELECT get_distance(point_variable, @POINTS_VAR)