写一个程序年度收入的emp并找到所得税

时间:2017-07-13 11:43:29

标签: sql plsql cursor

写一个程序年度收入的emp并找到员工表的所得税。

输出应为first_name,salary,ann_sal和tax。

  • i)如果annsal> 60000然后税收是收入的10%
  • ii)如果annsal> 100000然后税收是800卢比+收入的16%
  • iii)如果annsal> 140000然后税收是2500卢比+ 25%的收入

样本数据

FIRST_NAME              SALARY

Steven                    24000
Neena                     17000
Lex                       17000
Alexander                  9000
Bruce                      6000
David                      4800
Valli                      4800
Diana                      4200
Nancy                     12008
Daniel                     9000

这是我的计划:

DECLARE
    CURSOR income
    IS
        SELECT first_name,
            salary,
            salary*12 ann_sal
        FROM EMPLOYEES;
    z income%rowtype;
    ann_sal NUMBER(8,2);
    tax     NUMBER(8,2);
BEGIN
    OPEN income;
    LOOP
        FETCH income
        INTO z;
        EXIT
    WHEN income%notfound;
        IF ann_sal>60000 THEN
            tax  :=ann_sal*10/100;
        elsif ann_sal>100000 THEN
            tax     :=800+ann_sal*16/100;
        elsif ann_sal>140000 THEN
            tax     :=2500+ann_sal*25/100;
        ELSE
            tax:='';
        END IF;
        dbms_output.put_line(z.first_name || '  ' || z.salary || ' ' ||z.ann_sal|| '  ' || z.tax);
    END LOOP;
    CLOSE income;
END;  

就是这样。

2 个答案:

答案 0 :(得分:0)

你走了。我重写了代码。如果输出符合预期,请告诉我。

   DECLARE
        CURSOR income IS
          SELECT first_name,
                 salary,
                 salary * 12 ann_sal
          FROM   employees;
        z       income%ROWTYPE;
        tax     NUMBER(8, 2);
    BEGIN
        OPEN income;

        LOOP
            FETCH income INTO z;

            exit WHEN income%NOTFOUND;

            IF z.ann_sal > 60000 THEN
              tax := z.ann_sal * 10 / 100;
            ELSIF z.ann_sal > 100000 THEN
              tax := 800 + z.ann_sal * 16 / 100;
            ELSIF z.ann_sal > 140000 THEN
              tax := 2500 + z.ann_sal * 25 / 100;
            ELSE
              tax := '';
            END IF;

            dbms_output.Put_line(z.first_name
                                 || '  '
                                 || z.salary
                                 || ' '
                                 ||z.ann_sal
                                 || '  '
                                 || tax);
        END LOOP;

        CLOSE income;
    END; 

答案 1 :(得分:0)

你不需要PL / SQL。

select first_name
     , salary
     , salary * 12 ann_sal
     , case
           when salary * 12 > 140000 then 2500 + (salary * 12 * 25) / 100
           when salary * 12 > 100000 then  800 + (salary * 12 * 16) / 100
           when salary * 12 >  60000 then salary * 12 * 10 / 100
           else 0
       end as tax
from   employees;

请注意,超过100000的任何工资也超过60000,因此在原始代码中,您永远不会达到> 100000> 140000条件。我已经重新安排条件,以便从最高薪水中检查下来。

如果由于某种原因你真的需要它在PL / SQL循环中,则无需声明任何内容:

begin
    for r in (
        select first_name
             , salary
             , salary * 12 ann_sal
             , case
                   when salary * 12 > 140000 then 2500 + (salary * 12 * 25) / 100
                   when salary * 12 > 100000 then  800 + (salary * 12 * 16) / 100
                   when salary * 12 >  60000 then salary * 12 * 10 / 100
                   else 0
               end as tax
        from   employees
        order by first_name
    )
    loop
        dbms_output.put_line(rpad(r.first_name,15) || lpad(r.salary,9) || lpad(r.ann_sal,9) || lpad(r.tax,9));
    end loop;
end;