sql >> データベース >  >> RDS >> PostgreSQL

PostgreSQL関数の入力パラメーターとしてのユーザー定義型

    SQLクエリから関数を呼び出すには、次の例のように、パラメータをカスタムタイプにキャストする必要があります。

    select form_insertion(array[
        cast(row('Form 1', 1, current_date, 1, current_date, 'This is form 1', 
            array[
                cast(row('section-1', 'Section One', 1) as section),
                cast(row('section-2', 'Section Two', 2) as section),
                cast(row('section-3', 'Section Three', 3) as section)
            ]
        ) as form_details),
        cast(row('Form 2', 2, current_date, 1, current_date, 'This is form 2', 
            array[
                cast(row('section-2', 'Section Two', 2) as section),
                cast(row('section-3', 'Section Three', 3) as section)
            ]
        ) as form_details),
        cast(row('Form 3', 1, current_date, 1, current_date, 'This is form 3', 
            array[
                cast(row('section-1', 'Section One', 1) as section),
                cast(row('section-3', 'Section Three', 3) as section)
            ]
        ) as form_details)
    ])
    

    PostgreSQL配列には.COUNTプロパティがないことに注意してください。 array_upper 機能:

    for i IN 1..array_upper(formdetails, 1)
    LOOP 
       -- your code here
    END LOOP;
    

    PostgreSQL 9.1以降、FOREACHステートメントを使用して配列をループできます。

    create or replace function form_insertion(formdetails form_details[])
        returns varchar as $$
    declare
        detail form_details;
        sec section;
    begin
        foreach detail in array formdetails
        LOOP 
           RAISE NOTICE '%', detail.formName;
    
           foreach sec in array detail.sections
           LOOP
             raise NOTICE '%', sec.sectionName;
           END LOOP;
        END LOOP;
        return '';
    end;$$
        language plpgsql;
    



    1. PostgreSQL:日付に基づいてリピーターを特定する-結合またはウィンドウ関数?

    2. 大きなクエリを実行するときにメモリが不足していませんか?

    3. UNIONALLステートメントのMySQLCOUNT結果

    4. MySQL正規表現はUnicodeマッチングをサポートしていますか