错误:insert没有结果数据的目标

时间:2014-12-16 04:07:03

标签: postgresql

我有一个函数getLocationId。它在桌子上运行,位置,

CREATE TABLE IF NOT EXISTS Location ( 
     longitude double precision NOT NULL check( -180.0 < longitude AND longitude <= 180.0 ),
     latitude double precision NOT NULL check( -90.0 <= latitude AND latitude <= 90.0 ),
     altitude double precision  default(0), 
     unique(longitude,latitude,altitude),
     loc_id integer  DEFAULT nextval('loc_loc_id_seq'::regclass) 
);

CREATE OR REPLACE FUNCTION getLocationId( lng double precision, lat double precision,alt double precision ) RETURNS integer AS $gli$
DECLARE
        lid integer;
BEGIN
     SELECT loc_id into lid from Location where lng = longitude AND lat = latitude AND alt = altitude;
     if NOT FOUND then
        insert into Location VALUES ( lng , lat , alt ) RETURNING loc_id as lid;
     END IF;
     RETURN lid;
END;
$gli$  LANGUAGE plpgsql;

当我运行它时,我得到:

select * from getLocationId( -73.993953611111,40.7192777778,10.8);
ERROR:  query has no destination for result data
CONTEXT:  PL/pgSQL function "getlocationid" line 11 at SQL statement

有人可以提供帮助。

1 个答案:

答案 0 :(得分:2)

我在insert语句中使用了AS而不是INTO。它必须是:

... RETURNING loc_id INTO lid;