2017-08-03 11 views
0

だから、私は関数select_id_from_table(_t)を持っています。テーブルの特定の列(_t)を選択します。ここで、_tはパラメータとしてテーブル名です。postgresqlの更新関数でパラメータと参照を渡す

CREATE OR REPLACE FUNCTION myfunction(_u type1, _t type2) returns void as $$ 
BEGIN 
UPDATE (_u) set score=score+1 where _u.id in _t.id; 
END; 
$$ LANGUAGE plpgsql 

問題は、それが適切にパラメータを渡すことはできませんです。私は、関数はこのような何かを行う別の関数を作成したい.Now 私はSELECT select_id_from_table('tablename')のようにそれを呼び出します。また、type1とtype2はどうすればよいですか? _uと_tは両方とも表の名前です。私が試してみました:私も一時テーブルを作成してみました

$$begin 
create temp table lid as (select * from select_id_from_table(_t)); 
execute format ('update '||quote_ident(_u) ||' set score= score+1 where 
'||quote_ident(_u) ||'.id_ in (
select * from select_id_from_table ('||quote_ident(_t)||') as 
abc)'); 
end;$$ 

、その一時テーブルにselect_id_from_table(_t)を選択して、後でそれを参照してください。しかし、私はまだexecute format('')でそれを引用する方法を知らない。任意のアイデアをいただければ幸いです。

答えて

0

select_id_from_tableは、SETOF <something>を返す関数であり、戻り値の型をtextにキャストできるとします。

私はそれをテストしていないが、私はこれまで、それは同様のだろう。

DECLARE 
    in_clause text; 
BEGIN 
    SELECT string_agg(quote_literal(t::text), ', ') INTO in_clause 
     FROM select_id_from_table(_u) s(t); 
    EXECUTE format ('UPDATE %I 
        SET score = score + 1 
        WHERE %I.id_ =ANY (' || in_clause || ')', 
        _u, _u); 
END; 
+0

私はstring_agg(T ::テキスト、 '')、どこで(中%のI.id_に変更。.. )それは動作します。ありがとうございました! –

関連する問題