[pgsql-jp: 41060] 再起的なSQLで配列の型について

apostleofwhom apostleofwhom @ gmail.com
2012年 3月 1日 (木) 13:41:29 JST


ohara と申します。

https://www.sraoss.co.jp/technology/postgresql/8.4/
この「問い合わせ言語」のWITH RECURSIVEの項のあたりを
参考にして、以下のテーブルを作成しました。

create table tree(id text, parentid text);
insert into tree values
( '001',null),
( '002',   '001'),
( '003',   '001'),
( '004',   '002'),
( '005',   '002'),
( '006',   '003'),
( '007',   '006'),
( '008',   '006'),
('050',null),
('055',  '050'),
('056',  '050'),
('058',  '056');


id が text 型や varchar だとエラーにならないのですが、
id を varchar(12) や varchar(64) にすると、

ERROR:  recursive query "rec" column 4 has type character varying(12)[] 
in non-recursive term but type character varying[] overall
LINE 2:     select t.id, t.parentid, 1, array[t.id]

とエラーになります。SQL は以下となります。

with recursive rec(id, parentid, lv, path) as (
    select t.id, t.parentid, 1, array[t.id]
    from tree t
    where t.parentid is null
    union all
    select t.id, t.parentid, rec.lv + 1, rec.path || t.id
    from tree t, rec
    where t.parentid = rec.id
)
select id, parentid, lv, path
from rec order by path;

id を varchar(12) にしてもエラーにならないようにするには
どうすればよろしいでしょうか。

ご教授していただけますと幸いです。
よろしくお願いいたします。



-- 
apostleofwhom <apostleofwhom @ gmail.com>



pgsql-jp メーリングリストの案内