2016-07-21 59 views
0
CREATE OR REPLACE FUNCTION f_old_transactions (IN p_fromdate date, IN p_todate date, IN p_transtype varchar,IN OUT p_cancelled boolean, 
OUT p_transaction_date date, 
OUT p_type varchar, 
OUT p_description varchar, 
OUT p_amount numeric) 


RETURNS SETOF record AS 
$BODY$ 

declare lRunQuery text; 
declare lTotalRec record; 
declare lBranchList text[]; 
declare lTranstype text[]; 


BEGIN 

select into lTranstype 
    dt.type 

from 
    v_data_types dt; 


    lTranstype := regexp_split_to_array(p_transtype, ','); 

lrunquery := 'select 
it.transaction_date trandate, 
dt.type, 
it.description, 
ita.amount, 
it.cancelled 


from 
import_transaction it 
inner join import_transaction_account ita on it.import_transaction_id=ita.import_transaction_id 



where 
it.transaction_date >= ' || quote_literal(p_fromdate) || ' 
and it.transaction_date <= ' || quote_literal(p_todate) || ' 
and dt.type = any(' || quote_literall(p_transtype) || ') and'; 

if (p_cancelled = TRUE) then 
lrunquery := lrunquery || ' 
it.cancelled = ' || quote_literal(p_cancelled) || ''; 
else 
lrunquery := lrunquery || ' 
it.cancelled = ' || quote_literal(p_cancelled) || ''; 
end if; 



FOR lTotalrec in 
    execute lRunQuery 


LOOP 
p_transaction_date := ltotalrec.trandate; 
p_type :=ltotalrec.type; 
p_description :=ltotalrec.description; 
p_amount :=ltotalrec.amount; 
p_cancelled := ltotalrec.cancelled; 
return next; 
END LOOP; 

return ; 
end; 


$BODY$ 
LANGUAGE plpgsql IMMUTABLE 
COST 100 
ROWS 1000; 
ALTER FUNCTION f_old_transactions(date,date,varchar,boolean) OWNER TO "CompuLoanPostgres"; 

select * from f_old_transactions ('01-Jan-2010','31-Dec-2018','Receipt Cash','FALSE') 

私の配列の値は "{"で始まる必要があります。私の配列は、ビューから作成しようとしていますv_data_typeビューは、varchar型の列が1つだけで構成されています。Postgresql:配列の値は "{"またはディメンション情報で始まる必要があります

誰でも私のコードの問題がどこにあるのか教えてください。

は、私はあなたが間違っている何が起こっているのか特定するために知っている私たちに十分な情報を与えてくれたとは思わない、事前

答えて

0

でいただきありがとうございます。特に、スキーマやコンテンツのいずれの表がどのように見えるかはわかりません。それがなければ、自分のDBにテストケースを作成してデバッグすることはできません。

前記、私は特にlTranstype変数を中心に、カップルの事に気づいた:

  1. あなたは二回lTranstypeを割り当てています。最初にSELECT INTOと入力し、すぐにp_transtype引数から解凍した値に割り当てます。あなたがその変数で望むものが私にはっきりしていません。

  2. クエリを後で作成するときには、and dt.type = any(' || quote_literall(p_transtype) || ')が含まれます。問題は、p_transtypeがvarchar引数であり、配列のようにアクセスしようとしていることです。私はあなたがそれがand dt.type = any(' || quote_literall(lTranstype) || ')を読むことを望んでいると思うが、私は間違っている可能性がある。

私はあなたのタイプのエラーは、その第二の問題から来ていることを推測しているが、あなたは、この機能の異なる変数がために意図されているものを再評価する必要があるように思えます。がんばろう。

関連する問題