0

PL/SQL は初めてです。それは今まで順調でした。私は正常に動作するこのクエリをクエリしました。

declare
rec employees_practice%rowtype;
sam taxObligations%rowtype;
socialsecurity number;
rentallowance number;
transportation number;
taxableincome number;
incometaxliability number;
netpay number;
total number;
totaldeductions number;

    begin
    for rec in (select * from employees_practice)
    loop
    socialsecurity:=(5.5/100)*(rec.salary);
    rentallowance:=(20/100)*(rec.salary);
    if(rec.Category='S') 
    then transportation:= 150; 
    else transportation:=100;
    end if;

taxableincome:=rec.Salary-socialsecurity+rentallowance+transportation;

for sam in (select * from taxObligations) 
loop
if(taxableincome between sam.Minincome and sam.Maxincome)
then incometaxliability:= sam.MinimumBracketTax + (taxableincome-sam.Minincome)*(sam.TaxBracketRate/100);
else incometaxliability:=null;
end if;
end loop;
netpay:= taxableincome-incometaxliability;
total:= rec.Salary + rentallowance + transportation;

totaldeductions:=socialsecurity + incometaxliability;

-- Here, I used DBMS.... to give an output in different format.

end loop; 
end;

単一の SQL または PL/SQL クエリで呼び出すことができるように、上記のコードを含む関数を作成したいと考えています。それは私にとって頭痛の種でした。

4

2 に答える 2

1

パイプライン関数にすることができます:

まず、テーブル タイプが必要です。

create or replace type result_tab as table of varchar2(32767);

次に、コードをパイプライン関数にします。

create or replace function your_func return result_tab PIPELINED is

rec employees_practice%rowtype;
sam taxObligations%rowtype;
socialsecurity number;
rentallowance number;
transportation number;
taxableincome number;
incometaxliability number;
netpay number;
total number;
totaldeductions number;

    begin
    for rec in (select * from employees_practice)
    loop
    socialsecurity:=(5.5/100)*(rec.salary);
    rentallowance:=(20/100)*(rec.salary);
    if(rec.Category='S') 
    then transportation:= 150; 
    else transportation:=100;
    end if;

taxableincome:=rec.Salary-socialsecurity+rentallowance+transportation;

for sam in (select * from taxObligations) 
loop
if(taxableincome between sam.Minincome and sam.Maxincome)
then incometaxliability:= sam.MinimumBracketTax + (taxableincome-sam.Minincome)*(sam.TaxBracketRate/100);
else incometaxliability:=null;
end if;
end loop;
netpay:= taxableincome-incometaxliability;
total:= rec.Salary + rentallowance + transportation;

totaldeductions:=socialsecurity + incometaxliability;

-- Here, I used PIPE ROW() to give an output in different format.
pipe row('what ever you had in your dbms_output command');

end loop; 

return;

end your_func;

これで、次のように呼び出し/クエリを実行できます。

select * from table(your_func)
于 2012-07-02T10:41:02.063 に答える
0

すべてが正常に機能している場合は、INパラメーターとしてCURSORを使用してストアドFUNCTIONですべてを実行し、NUMBERを返します。変数タイプとカーソルについて学びます。Oracleのドキュメントは、自分でそれを行うのに十分なはずです。

その後、コードに問題が発生した場合は、ここに投稿してください。そうすれば、きっと助けになります。

于 2012-07-02T10:23:44.927 に答える