void函数抛出'查询没有结果数据的目的地'错误

时间:2015-07-22 08:17:00

标签: sql postgresql

我有一个带有void返回类型的函数(包括整个函数):

CREATE OR REPLACE FUNCTION insert_avg_prices(p_county character varying, p_municipality character varying, p_district character varying, p_neighbourhood character varying, p_street character varying, p_number character varying, p_avg_rent decimal, p_avg_sale decimal)
 RETURNS VOID AS
$BODY$
DECLARE
    _precint_id integer;
    _city_id integer;
    _parish_id integer;
    _county_id integer;

    _address_id integer;

BEGIN
    SELECT place_precint.precint_id INTO _precint_id FROM place_precint WHERE place_precint.name = p_neighbourhood;
    SELECT place_city.city_id INTO _city_id FROM place_city WHERE place_city.name = p_district;
    SELECT place_parish.parish_id INTO _parish_id FROM place_parish WHERE place_parish.name = p_municipality;
    SELECT place_county.county_id INTO _county_id FROM place_county WHERE place_county.name = p_county;

    SELECT full_addresses.f_address_id FROM full_addresses 
    WHERE  full_addresses.county_id = _county_id AND
           full_addresses.parish_id = _parish_id AND
           full_addresses.city_id = _city_id AND
           full_addresses.precint_id = _precint_id AND
           full_addresses.street_name = p_street AND
           full_addresses.number = p_number;

    IF _address_id IS NULL THEN
        INSERT INTO full_addresses(f_address_id,
                     county_id,
                     parish_id,
                     city_id,
                     precint_id,
                     street_name,
                     number)
        VALUES (nextval('full_addresses_f_address_id_seq'),
            _county_id,
            _parish_id,
            _city_id,
            _precint_id,
            p_street,
            p_number);

        SELECT currval('full_addresses_f_address_id_seq') INTO _address_id;
    END IF;

    INSERT INTO building_prices(building_id, 
                    address_id, 
                    avg_sau_sale_price, 
                    avg_sau_rent_price) 
    VALUES (nextval('building_prices_building_id_seq'),
        _address_id,
        p_avg_sale,
        p_avg_rent);
    RETURN;
END;

$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

如果我用这样的select语句调用它:

SELECT insert_avg_prices(...);

我收到错误:

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function insert_avg_prices(character varying,character varying,character varying,character varying,character varying,character varying,numeric,numeric) line 16 at SQL statement
********** Error **********

ERROR: query has no destination for result data
SQL state: 42601
Hint: If you want to discard the results of a SELECT, use PERFORM instead.

我的CREATE FUNCTION语句没有错误。如何使它工作?

1 个答案:

答案 0 :(得分:3)

在你的函数中你有一个SELECT语句,但该语句无处可放。

显然错误在这里:

SELECT full_addresses.f_address_id FROM full_addresses WHERE ...

应该是

SELECT full_addresses.f_address_id INTO _address_id FROM full_addresses WHERE ...

请注意,PL / pgSQL是一种解释型语言,这意味着函数体不会像在Pascal或C ++等编译编程语言中那样评估语法完整性。因此,您可以编写一个函数,该函数仅在调用时显示任何错误。