【问题标题】:Creating and calling PL/SQL functions创建和调用 PL/SQL 函数
【发布时间】:2012-07-02 18:00:42
【问题描述】:

我是 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 查询来调用它。这让我很头疼。

【问题讨论】:

    标签: sql database plsql oracle11g plsqldeveloper


    【解决方案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 行错误:PL/SQL:编译单元分析终止
    • radashk,这就是我一直在尝试做的。
    【解决方案2】:

    如果现在一切正常,请尝试在存储的 FUNCTION 中执行所有操作,并将 CURSOR 作为 IN 参数以返回 NUMBER。了解变量类型和游标。 Oracle docs 应该足以让您自己完成。

    之后,如果您的代码遇到问题,请在此处发布,您一定会得到帮助。

    【讨论】:

      猜你喜欢
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      相关资源
      最近更新 更多