Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Membuat dan memanggil fungsi PL/SQL

Anda dapat menjadikannya fungsi berpipa :

pertama-tama Anda memerlukan jenis tabel:

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

Kemudian, buat kode Anda menjadi fungsi pipeline:

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;

Sekarang, Anda dapat menelepon / menanyakannya seperti ini:

select * from table(your_func)


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana cara mengurutkan berdasarkan angka terlebih dahulu dengan kueri Oracle SQL?

  2. masalah dengan fungsi to_date dengan sysdate

  3. Cara membuat grafik menggunakan PHP dari oracle

  4. Sisipan Massal Oracle Menggunakan Pengembang SQL

  5. EF 4, cara menambahkan kelas parsial