4

私はPostgresql 9.3を使用しており、以下のように関数を書きました:

    create or replace function test(building text,floor text) returns void as $$
    Declare
    id integer;
    num integer := 0;
    Begin

    num=num+100

    id :=select to_number(
          (select 
              (select code from buildings where name=building) || floor 
              || (select num::text)),'99999999'
    );

    update table set col1=id;

    End;
    $$
    language plpgsql;

私が期待しているのは、クエリからのid数値が変数に割り当てられることです。example: 12502100select to_number(...)

ただし、以下のエラーが発生しました

ERROR:  syntax error at or near ":="
LINE 10: source :=(select code from buildings where name='I3')

クエリ結果 (いくつかの文字列操作を含む) を変数 id に割り当てるにはどうすればよいですか?

私もSelect Into id...方法で失敗しました。

4

1 に答える 1

5

SELECT関数の評価に使用する必要はありません。

id := to_number((SELECT code FROM buildings WHERE name = building) 
                                                      || floor || num::text,
                '999999999');

他の可能性(通常はより良い)は、式リスト(結果フィールドリスト)で関数を使用することです

id := (SELECT to_number(code || floor || num::text, '99999999') 
          FROM buildings WHERE name = building)

SELECT関数や変数の評価ではなく、データへのクエリが必要な場合にのみ使用してください。

于 2013-10-09T09:05:30.507 に答える