2016-09-01 13 views
1

誰かがこれを説明できますか? (evrythingは下の例にあります!)。私はpostgresql 9.4を使用します。 警告!以下のコードでは、 "オブジェクト型"コンポジット型と "example_data"テンポラリテーブルを削除します。データベースにこれらの型がないことを確認してください...もし、ドロップコマンドにコメントし、必要に応じてコメントを外してください。 ..Postresql:複合型と選択する

--drop type if exists object_type; 
--drop table if exists example_data; 

create type object_type as (
     id text 
     ,value text); 

create temporary table example_data as (
     select 
      'id1'::text as id, 
      'example value'::text as value); 

do $$ 
declare 
    my_object object_type; 
begin 

    -- The fiolowing doesn't work, we can't use INTO with the whole object 
    -- Some how, it tries to put the whole object into the first attribute: 
    select (id,value)::object_type 
    into my_object 
    from example_data; 

    raise warning 'ID: %, VALUE: %', my_object.id, my_object.value; 

    -- What a shame! It would have been so much more convenient than the following:  
    -- to feed the object we need to repeat each one of the attribute in the INTO section: 
    select id, value 
    into my_object.id, my_object.value 
    from example_data; 

    raise warning 'ID: %, VALUE: %', my_object.id, my_object.value; 

/* 
In that example it is not so bad, but when you have very large object, it is very ugly to repeat each one 
of the attribute, for example: 
select (att1, att2, att3, att4, att5, att6, att7, att8, att9, att10)::object_type2 into my_object2 

and, (very heavy): 
select att1, att2, att3, att4, att5, att6, att7, att8, att9, att10 
into my_object2.att1, my_object2.att2, my_object2.att3, my_object2.att4, my_object2.att5, my_object2.att6, 
    my_object2.att7, my_object2.att8, my_object2.att9, my_object2.att10 

*/ 

end $$; 

--drop type if exists object_type; 
--drop table if exists example_data; 

答えて

2

これは本当に簡単です:

... 
select id, value 
into my_object 
from example_data; 
... 
+0

そう簡単に、私は恥ずかしいです... –

関連する問題