2012-07-02 34 views
0

我是PL/SQL的新手。現在情況一直很好。 我有查詢這個查詢哪些工作正常。創建和調用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查詢來調用它。這對我來說很頭疼。

回答

0

如果現在一切正常,請嘗試使用CURSOR作爲IN參數在存儲的FUNCTION中完成所有操作,以返回NUMBER。瞭解變量類型和遊標。 Oracle docs應該足以讓你自己做。

之後,如果您遇到代碼問題,請在此處發帖,您當然會獲得幫助。

1

你可以把它pipelined function

首先您需要的表類型:

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) 
+0

我試過了,得到這個錯誤。 錯誤在第0行:PL/SQL:編譯單元分析終止 –

+0

radashk,這正是我一直在嘗試做的。 –