お世話になっております。
PostgreSQLでファンクションを作成しており、引数に応じてSELECTの内容を変えたいです。
hogeテーブルのcol1-co5はいずれもvarchar(4000)です。
create or replace function test(p_type in integer) returns table( col1 hoge.col1%type, col2 hoge.col2%type, col3 hoge.col3%type, col4 hoge.col4%type, col5 hoge.col5%type) as $$ declare v_sql text; begin if p_type = 1 then v_sql:='select col1, col2, col3, col4, col5 from hoge order by col1'; else v_sql:='select col1, col2, col3 from hoge order by col1'; end if; return query execute v_sql; end $$ language plpgsql;
このようなfunctionを定義し実行時の引数に1以外を与えたとき、下記エラーが発生します。
ERROR: structure of query does not match function result type DETAIL: Number of returned columns (3) does not match expected column count (5).
そこで結果が5列になるようelse以下のSQLを変更してみたのですが、やはりエラーとなります。
v_sql:='select col1, col2, col3, null, null from hoge order by col1'; v_sql:='select col1, col2, col3, ''a'', ''b'' from hoge order by col1';
hogeテーブルの表構造からするとエラーにはならないと思うのですが、エラーになる理由と回避方法がありましたらご教示下さい。宜しくお願い致します。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/05 08:18 編集