Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
Postgresql:错误:列";“梅瓦尔”;不存在,而myvar是一个变量_Postgresql_Postgresql 9.4 - Fatal编程技术网

Postgresql:错误:列";“梅瓦尔”;不存在,而myvar是一个变量

Postgresql:错误:列";“梅瓦尔”;不存在,而myvar是一个变量,postgresql,postgresql-9.4,Postgresql,Postgresql 9.4,我试图在plsql中创建一个函数,以查找从作为参数传递到函数的用户1英里范围内的5个用户 这是我的代码: CREATE OR REPLACE FUNCTION testfunction (integer) RETURNS integer as $$ DECLARE myvar integer := $1; mylon double precision; mylat double precision; lon1 float; lon2

我试图在plsql中创建一个函数,以查找从作为参数传递到函数的用户1英里范围内的5个用户

这是我的代码:

    CREATE OR REPLACE FUNCTION testfunction (integer) RETURNS integer as $$
    DECLARE
    myvar integer := $1;
    mylon double precision; 
    mylat double precision; 
    lon1 float; 
    lon2 float; 
    lat1 float; 
    lat2 float;
    BEGIN
    raise notice 'help = %', $1; --just for testing
    raise notice 'myvar = %', myvar; --again for testing
    select cr.last_known_longitude, cr.last_known_latitude into mylon, mylat from current_reg as cr where userid = myvar;
    lon1 = mylon - 1 / abs(cos(radians(mylat))*69);
    lon2 = mylon + 1/abs(cos(radians(mylat))*69);
    lat1 = mylat - (1/69);
    lat2 = mylat + (1/69);
    create view myview as
            select destination.userid, 3956*2*asin(sqrt(power(sin((origin.last_known_latitude-destination.last_known_latitude)*pi()/180 / 2), 2) + cos(origin.last_known_latitude* pi()/180) * cos(destination.last_known_latitude* pi()/180) * power(sin((origin.last_known_longitude-destination.last_known_longitude) * pi()/180 /2), 2))) as distance
            from current_reg destination, current_reg origin
            where origin.userid = myvar
            and destination.last_known_longitude between lon1 and lon2
            and destination.last_known_latitude between lat1 and lat2
            having distance < 1 order by distance limit 5;
    return 0;
    END; 
    $$ LANGUAGE 'plpgsql';
    Select testfunction(7);
创建或替换函数testfunction(integer)返回整数作为$$
声明
myvar整数:=$1;
迈伦双精度;
迈拉双精度;
lon1浮球;
lon2浮球;
lat1浮子;
lat2浮子;
开始
提出通知“帮助=%”,1美元--只是为了测试
提出通知“myvar=%”,myvar--再次进行测试
选择cr.last_known_经度,cr.last_known_纬度到mylon,mylat从当前_注册为cr,其中userid=myvar;
lon1=mylon-1/abs(cos(弧度(mylat))*69);
lon2=mylon+1/abs(cos(弧度(mylat))*69);
lat1=mylat-(1/69);
lat2=迈拉特+(1/69);
创建视图myview作为
选择destination.userid,3956*2*asin(sqrt(功率(sin((原点、最后一个已知纬度)destination.最后一个已知纬度)*pi()/180/2),2)+cos(原点、最后一个已知纬度*pi()/180)*cos(destination.最后一个已知纬度*pi()/180)*功率(sin sin(原点、最后一个已知经度、最后一个已知纬度)*pi()/180/2)作为距离
从当前\u reg目的地、当前\u reg来源
其中origin.userid=myvar
和destination.last_known_lon1和lon2之间的经度
和目的地。lat1和lat2之间的最后已知纬度
距离小于1个数量级,距离限制为5;
返回0;
结束;
$$语言“plpgsql”;
选择testfunction(7);
其中,current_reg是一个以userid、last_known_纬度、last_known_经度为列的表。作为整数传递给函数的参数是我希望从其位置(纬度和经度)查找英里范围内用户的用户ID

我得到以下错误:

NOTICE:  help = 7
NOTICE:  myvar = 7

ERROR:  column "myvar" does not exist
LINE 1: ...ination, current_reg origin where origin.userid = myvar and de...
                                                             ^
QUERY:  create view myview as select destination.userid, 3956*2*asin(sqrt(power(sin((origin.last_known_latitude-destination.last_known_latitude)*pi()/180 / 2), 2) + cos(origin.last_known_latitude* pi()/180) * cos(destination.last_known_latitude* pi()/180) * power(sin((origin.last_known_longitude-destination.last_known_longitude) * pi()/180 /2), 2))) as distance from current_reg destination, current_reg origin where origin.userid = myvar and destination.last_known_longitude between lon1 and lon2 and destination.last_known_latitude between lat1 and lat2 having distance < 1 order by distance limit 5
CONTEXT:  PL/pgSQL function testfunction(integer) line 19 at SQL statement
********** Error **********

ERROR: column "myvar" does not exist
SQL state: 42703
Context: PL/pgSQL function testfunction(integer) line 19 at SQL statement
注意:help=7
注意:myvar=7
错误:“myvar”列不存在
第1行:…初始化,当前_reg origin,其中origin.userid=myvar和de。。。
^
查询:创建视图myview作为select destination.userid,3956*2*asin(sqrt(功率(sin((原点.上次已知纬度-destination.last已知纬度)*pi()/180/2),2)+cos(原点.上次已知纬度*pi()/180)*cos(destination.last已知纬度*pi()/180)*功率(sin sin(原点.上次已知经度-destination.last已知经度)*pi()/180/2))作为与当前_reg目的地的距离,当前_reg原点,其中origin.userid=myvar和destination.last_known_lon1和lon2之间的经度和destination.last_known_lation lat1和lat2之间的纬度,距离小于1,距离限制为5
上下文:PL/pgSQL函数testfunction(整数)SQL语句第19行
**********错误**********
错误:“myvar”列不存在
SQL状态:42703
上下文:PL/pgSQL函数testfunction(整数)SQL语句第19行
既然“myvar”是一个变量,为什么它希望它是一个列呢

在@a_horse_(没有名字)的帮助下,这是修改过的代码

    CREATE OR REPLACE FUNCTION testfunction (p_userid integer) 
    RETURNS table (userid integer, distance float) 
    AS 
    $$
    DECLARE
    mylon double precision; 
    mylat double precision; 
    lon1 float; 
    lon2 float; 
    lat1 float; 
    lat2 float;
    BEGIN
    raise notice 'help = %', p_userid; --just for testing

    select cr.last_known_longitude, cr.last_known_latitude 
    into mylon, mylat 
    from current_reg as cr 
    where cr.userid = p_userid;

    lon1 = mylon - 1 / abs(cos(radians(mylat))*69);
    lon2 = mylon + 1/abs(cos(radians(mylat))*69);
    lat1 = mylat - (1/69);
    lat2 = mylat + (1/69);

    return query 
       select destination.userid, 
       3956*2*asin(sqrt(power(sin((origin.last_known_latitude-destination.last_known_latitude)*pi()/180 / 2), 2) + cos(origin.last_known_latitude* pi()/180) * cos(destination.last_known_latitude* pi()/180) * power(sin((origin.last_known_longitude-destination.last_known_longitude) * pi()/180 /2), 2)))
       as distance 
       from current_reg as destination JOIN current_reg as origin 
       where origin.userid = p_userid
       and destination.last_known_longitude between lon1 and lon2 
       and destination.last_known_latitude between lat1 and lat2 
       having distance < 1 
       order by distance limit 5;

    END; 
    $$ LANGUAGE plpgsql;
    Select userid from testfunction(4);
创建或替换函数testfunction(p_userid integer)
返回表(用户标识整数、距离浮点)
作为
$$
声明
迈伦双精度;
迈拉双精度;
lon1浮球;
lon2浮球;
lat1浮子;
lat2浮子;
开始
发出通知“help=%”,p_userid--只是为了测试
选择cr.last\u known\u经度、cr.last\u known\u纬度
迈伦,迈拉
从当前注册为cr
其中cr.userid=p_userid;
lon1=mylon-1/abs(cos(弧度(mylat))*69);
lon2=mylon+1/abs(cos(弧度(mylat))*69);
lat1=mylat-(1/69);
lat2=迈拉特+(1/69);
返回查询
选择destination.userid,
3956*2*asin(sqrt(功率(sin((原点、上次已知纬度-目的地、上次已知纬度)*pi()/180/2),2)+cos(原点、上次已知纬度*pi()/180)*cos(目的地、上次已知纬度*pi()/180)*功率(sin((原点、上次已知经度-目的地、上次已知经度)*pi()/180/2)))
作为距离
从当前\u reg作为目标加入当前\u reg作为源
其中origin.userid=p_userid
和destination.last_known_lon1和lon2之间的经度
和目的地。lat1和lat2之间的最后已知纬度
距离小于1的
按距离限制订购5件;
结束;
$$语言plpgsql;
从testfunction(4)中选择userid;
我现在得到以下错误: 错误:在“where”处或附近出现语法错误 第29行:其中origin.userid=p_userid
^

如果要返回查询结果,需要在PL/pgSQL中使用
返回查询。不能在DDL语句中使用这样的变量。为函数的每次调用创建一个视图是一个非常糟糕的主意。更重要的是:第二次调用时,函数将失败,因为视图已经存在

根据你所写的,我认为你想要这样的东西:

CREATE OR REPLACE FUNCTION testfunction (p_userid integer) 
  RETURNS table (userid integer, distance float) 
AS 
$$
DECLARE
  mylon double precision; 
  mylat double precision; 
  lon1 float; 
  lon2 float; 
  lat1 float; 
  lat2 float;
BEGIN
  raise notice 'help = %', p_userid; --just for testing

  select cr.last_known_longitude, cr.last_known_latitude 
      into mylon, mylat 
  from current_reg as cr 
  where userid = p_userid;

  lon1 = mylon - 1 / abs(cos(radians(mylat))*69);
  lon2 = mylon + 1/abs(cos(radians(mylat))*69);
  lat1 = mylat - (1/69);
  lat2 = mylat + (1/69);

  return query 
    select destination.userid, 
           3956*2*asin(sqrt(power(sin((origin.last_known_latitude-destination.last_known_latitude)*pi()/180 / 2), 2) + cos(origin.last_known_latitude* pi()/180) * cos(destination.last_known_latitude* pi()/180) * power(sin((origin.last_known_longitude-destination.last_known_longitude) * pi()/180 /2), 2))) as distance 
    from current_reg destination, 
         current_reg origin 
    where origin.userid = p_userid
      and destination.last_known_longitude between lon1 and lon2 
      and destination.last_known_latitude between lat1 and lat2 
    having distance < 1 
    order by distance limit 5;

END; 
$$ LANGUAGE plpgsql;
注意该函数位于
FROM
子句中,而不是
SELECT
列表中

看起来仍然可疑的事情:

  • 您没有从当前的\u reg目的地连接这两个表,当前的\u reg origin
  • 会在这两个表之间创建一个交叉连接。另一个很好的例子是,为什么在where子句中使用显式的
    连接
    优于旧的隐式连接
  • 您正在使用
    具有
    ,但未使用任何聚合

  • 如果要返回查询结果,需要在PL/pgSQL中使用
    returnquery
    。不能在DDL语句中使用这样的变量。为函数的每次调用创建一个视图是一个非常糟糕的主意。更重要的是:第二次调用时,函数将失败,因为视图已经存在

    select *
    from testfunction(1);