2015-09-04 78 views
2

嗨,我正在創建一個插入元數據的過程。我創建了類型,並且我在另一個類型中包含了1個類型,並且在我正在迭代它以獲取值的過程中。由於我是PostgreSQL的新手,任何人都可以幫助我瞭解如何調用這個過程。輸入參數的類型爲用戶定義類型作爲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端調用。任何幫助將不勝感激。

回答

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功能:

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; 

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

因爲PostgreSQL 9.1,你可以通過數組使用foreach語句循環

相關問題