2016-12-22 240 views
1

私はpostgresとプログラミングには新しく、これについての解決策をすでに探していましたが、それを得ることができませんでした。私は国を呼び出すたびに、その国のすべての顧客に関する情報を返す関数を作成しようとしています。これがポップアップするエラーです。私は本当にこれを求めて申し訳ありませんが、私は昨日からここにこだわってきました。POSTGRESQL-クエリに結果データの宛先がありません

create or replace function country(in_parameter text,out out_res refcursor) as $$ 
begin 
open out_res for 
select customer_id, customer.first_name, customer.last_name 

from customer 

inner join address on customer.address_id = address.address_id 
inner join city on address.city_id = city.city_id 
inner join country on city.country_id = country.country_id 

where country = '$1'; 
end; 
$$ 

language plpgsql; 
+1

あなたはクエリを実行していますが、プロキシから何も返されません。どのようにして 'language sql'を使用するのかについては、ここの例で示されています。https://www.postgresql.org/docs/9.2/static/xfunc-sql.html – Glenn

+0

これをsqlに変更しました。これがエラーをポップアップ表示します。または近い "選択" 行5:select customer_id、customer.first_name、customer.last_name ^ 本当に混乱している – kimdasuncion12

+0

例を注意深く見てください。 35.4.4にはシナリオに合った例があります。最初に実行してから、必要なものに変更することができます。この例の 'language sql'は' begin'や 'end'を持たず、' out'パラメータを持っています。 – Glenn

答えて

5

PL/pgSQL関数でselect文を実行する場合は、クエリの結果をいくつかの変数(=送り先)に配置する必要があります。次に、関数内の変数を操作します。また、RETURNステートメントが必要です。

create or replace function country(text) returns text as $$ 
declare     -- declare some variables 
    id integer; 
    fname text; 
    lname text; 
begin 
    select customer_id, customer.first_name, customer.last_name 
    into id, fname, lname -- store query results in variables 
    from customer 
    inner join address on customer.address_id = address.address_id 
    inner join city on address.city_id = city.city_id 
    inner join country on city.country_id = country.country_id 
    where country = $1;  -- don't quote parameter references 

    -- do something with the variables and return a value from the function 
    return format('%s: %s %s', id, upper(lname), fname); 
end; 
$$ language plpgsql; 

上記はクエリが単一の行を返す場合にのみ機能することに注意してください。クエリで複数の行が返された場合は、関数内でuse a loopを使用できます。

create or replace function country(text) 
returns table (id integer, first_name varchar, last_name varchar) as $$ 
begin 
    return query 
    select customer_id, customer.first_name, customer.last_name 
    from customer 
    inner join address on customer.address_id = address.address_id 
    inner join city on address.city_id = city.city_id 
    inner join country on city.country_id = country.country_id 
    where country = $1; 
end; 
$$ language plpgsql; 

しかし、のようなEvan Carrollは、あなたがそれを返す前にデータを修正するためにPL/pgSQL関数を必要としない限り、あなたはシンプルでオフに優れている、言った:でもシンプルな、あなただけそうのようなクエリから結果を返すことができますビュー。

+0

詳細な回答ありがとうございました! – kimdasuncion12

0

が与えられた関数の結果を得るために、以下の使用してください:ここで

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 country(text) line 5 at SQL statement

機能です。通常、これは VIEWです。

CREATE VIEW myView AS 
    SELECT customer_id, customer.first_name, customer.last_name 
    FROM customer 
    INNER JOIN address USING (address_id) 
    INNER JOIN city USING (city_id) 
    INNER JOIN country USING (country_id); 

次に、あなたは、あなたがこの機能を作ることに主張する、とあなたはいけない場合、あなたはそれLANAGUAGE SQLなくLANGUAGE plppsql確認する必要があり、そのすべての言っ

SELECT * FROM myView WHERE country = ? 

を行います。

+0

こんにちはRiya私はそれを試して、それはあなたが私を助けてくださいこの結果を返した? – kimdasuncion12

+0

国 -------------------- <無名ポータル1> – kimdasuncion12

0

これは、SQLのために正常ではない..です

create or replace function country(text) returns text as $$ 
begin 


    select customer_id, customer.first_name, customer.last_name 

    from customer 

    inner join address on customer.address_id = address.address_id 
    inner join city on address.city_id = city.city_id 
    inner join country on city.country_id = country.country_id 

    where country = '$1'; 
    end; 
    $$ 

    language plpgsql; 
0

彼女が選択クエリと選択クエリ後RETURN MYCURSOPEN MYCURSを使用する場合それは私の同僚のために働きました。

関連する問題