【问题标题】:user defined type as input parameters in PostgreSQL function用户定义类型作为 PostgreSQL 函数中的输入参数
【发布时间】:2019-05-17 01:07:17
【问题描述】:

您好,我正在创建一个用于插入元数据的过程。我创建了类型,并在另一种类型中包含了一种类型,并且在过程中我正在迭代它以获取值。由于我是 PostgreSQL 新手,任何人都可以帮助我了解如何调用该过程。输入参数是类型

Create Type Form_details as(
                     formName character varying(100),
                     submittedBy numeric,
                      createdDate date,    
                     updatedBy numeric,
                     updatedDate date,
                     comments character varying(500),
                  Sections Section[]  

)

create type Section as (
                           sectionName character varying(100),
                           sectionLabel character varying(100),                           
                           sectionOrder numeric                           



                     )

我写的程序是

    CREATE OR REPLACE FUNCTION form_insertion(formdetails form_details[])
  RETURNS character varying AS
$BODY$

DECLARE 
         form_details_seq integer;
         section_seq integer;
         formName character varying(100);
         submittedBy numeric;
         createdDate date;    
         comments character varying(500);
                formStatusId numeric;

         sectionOrder numeric;
         sectionName character varying(100);
         sectionLabel character varying(100);
         attributeId numeric;

         I integer;
         J integer;
begin
FOR I IN 1..formdetails.COUNT

LOOP

formName             :=formdetails[I].formName;
formStatusId         :=formdetails[I].formStatusId;
comments             :=formdetails[I].comments;
  RAISE NOTICE '%', formName;

  FOR J IN 1..formdetails.Section.COUNT
   LOOP

   sectionName             :=formdetails[I].Section[J].sectionName;
 RAISE NOTICE '%', sectionName;

   END LOOP ;


END LOOP;

Return formName,sectionName;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

这不是完整的程序。但我正在尝试对此进行测试。您能否让我知道我的方法是否正确以及如何从数据库端对其进行测试。我将如何传递这个参数。顺便说一下,我创建的类型来自 Java 对象。此过程将从 Java 端调用。任何帮助将不胜感激。

【问题讨论】:

    标签: postgresql plpgsql


    【解决方案1】:

    要从 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-01
      • 2021-03-03
      • 2013-02-26
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 2021-04-05
      • 2019-10-19
      相关资源
      最近更新 更多