Home » SQL & PL/SQL » SQL & PL/SQL » net salary calculation (11g oracle)
net salary calculation [message #651074] Tue, 10 May 2016 20:16 Go to next message
nath123
Messages: 19
Registered: May 2016
Location: united states
Junior Member
i want to get the net salary for employees whose id are less than 140

ORA-01422 is the error , how should i alter the code ?

declare
ename varchar(31);
basic number(8,2);
salary number(8,2);
--salary1 number;
PF number;
HRA number;
DA number;
net number;
begin
select last_name,salary into ename, basic from employees where employee_id <140;
basic := salary;
DA := salary*(0.3);
HRA :=salary*(.10);
if (salary <8000) then PF :=salary *(0.07);
elsif( salary>=8000 and salary <=16000) then PF:=salary*(.16);
elsIF SQL%NOTFOUND THEN
        DBMS_OUTPUT.PUT_LINE('SQL DATA NOT FOUND');
end if;
net := DA+HRA+basic-PF;
dbms_output.put_line(net);
dbms_output.put_line(ename);
end
;

Re: net salary calculation [message #651075 is a reply to message #651074] Tue, 10 May 2016 20:30 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Re: net salary calculation [message #651076 is a reply to message #651075] Tue, 10 May 2016 20:33 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>select last_name,salary into ename, basic from employees where employee_id <140;
ename & basic are scalar variables which can only hold a single value.
This SELECT statement returns more than one row, so this error is thrown

some kind of cursor LOOP must be coded to avoid this error.
Re: net salary calculation [message #651077 is a reply to message #651076] Tue, 10 May 2016 20:38 Go to previous messageGo to next message
nath123
Messages: 19
Registered: May 2016
Location: united states
Junior Member
ok thankyou
Re: net salary calculation [message #651078 is a reply to message #651076] Tue, 10 May 2016 20:55 Go to previous messageGo to next message
nath123
Messages: 19
Registered: May 2016
Location: united states
Junior Member
i tried thrice, can you please suggest where i am missing now .

I see the plsql procedure is correct ,but no result

declare
ename varchar(31);
basic number(8,2);
salary number(8,2);
--salary1 number;
PF number;
HRA number;
DA number;
net number;
cursor c1 is
select last_name,salary into ename, basic from employees where employee_id <110;
begin
basic := salary;
DA := salary*(0.3);
HRA :=salary*(.10);
for i in c1 loop
if (salary <8000) then PF :=salary *(0.07);
elsif( salary>=8000 and salary <=16000) then PF:=salary*(.16);
elsIF SQL%NOTFOUND THEN
        DBMS_OUTPUT.PUT_LINE('SQL DATA NOT FOUND');
end if;
net := DA+HRA+basic-PF;
end loop;
dbms_output.put_line(net);
dbms_output.put_line(ename);
end
;
Re: net salary calculation [message #651079 is a reply to message #651078] Tue, 10 May 2016 21:19 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
https://www.google.com/webhp?hl=en&tab=ww#hl=en&q=pl%2Fsql+cursor+example
Re: net salary calculation [message #651080 is a reply to message #651079] Tue, 10 May 2016 22:19 Go to previous messageGo to next message
nath123
Messages: 19
Registered: May 2016
Location: united states
Junior Member
yeah , i knew there is issue in passing cursor variables
Re: net salary calculation [message #651082 is a reply to message #651080] Tue, 10 May 2016 22:34 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
in your last posted code you never OPEN any cursor or FETCH any row.
Re: net salary calculation [message #651087 is a reply to message #651078] Wed, 11 May 2016 00:23 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
I see the plsql procedure is correct ,but no result


First make sure you executed:
set serveroutput on


Format your code means INDENT it.
If you don't write readable code, you will never write correct code.

Then all your figures are NULL and so you don't see anything:
declare
ename varchar(31);     <--- value NULL
basic number(8,2);     <--- value NULL
salary number(8,2);    <--- value NULL
--salary1 number;
PF number;             <--- value NULL
HRA number;            <--- value NULL
DA number;             <--- value NULL  
net number;            <--- value NULL
cursor c1 is
select last_name,salary into ename, basic from employees where employee_id <110;
begin
basic := salary;       <--- value NULL
DA := salary*(0.3);    <--- value NULL
HRA :=salary*(.10);    <--- value NULL
for i in c1 loop
if (salary <8000) then PF :=salary *(0.07);                     <--- test NULL = NOT TRUE
elsif( salary>=8000 and salary <=16000) then PF:=salary*(.16);  <--- test NULL = NOT TRUE
elsIF SQL%NOTFOUND THEN                                         <--- test FALSE, you can never reach this
        DBMS_OUTPUT.PUT_LINE('SQL DATA NOT FOUND');                  as cursor loop only returns existing rows
end if;
net := DA+HRA+basic-PF; <--- value NULL
end loop;
dbms_output.put_line(net);   <--- display NULL, so nothing
dbms_output.put_line(ename); <--- display NULL, so nothing
end
;

Values returned by the cursor are found in i record and so are named i.last_name and i.salary.

[Updated on: Wed, 11 May 2016 02:19]

Report message to a moderator

Re: net salary calculation [message #651122 is a reply to message #651087] Wed, 11 May 2016 14:38 Go to previous messageGo to next message
nath123
Messages: 19
Registered: May 2016
Location: united states
Junior Member
yeah got it thankyou

declare
ename varchar(31);
basic number(8,2);
salary number(8,2);
--salary1 number;
PF number;
HRA number;
DA number;
net number;
cursor c1 is
select last_name,salary into ename, basic from employees where employee_id <102;
begin
for i in c1 loop
basic := i.salary;
DA := i.salary*(0.3);
HRA :=i.salary*(.10);
if (i.salary <4000) then PF :=i.salary *(0.07);
elsif( i.salary>=4000 and i.salary <=24000) then PF:=i.salary*(.16);
elsIF SQL%NOTFOUND THEN
        DBMS_OUTPUT.PUT_LINE('SQL DATA NOT FOUND');
end if;
net := DA+HRA+basic-PF;
        exit when sql%notfound;
dbms_output.put_line(net);
--dbms_output.put_line(basic);
dbms_output.put_line(i.last_name);
end loop;
end
;
Re: net salary calculation [message #651125 is a reply to message #651122] Wed, 11 May 2016 14:44 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

DECLARE 
    ename  VARCHAR(31); 
    basic  NUMBER(8, 2); 
    salary NUMBER(8, 2); 
    --salary1 number; 
    pf     NUMBER; 
    hra    NUMBER; 
    da     NUMBER; 
    net    NUMBER; 
    CURSOR c1 IS 
      SELECT last_name, 
             salary 
      INTO   ename, basic 
      FROM   employees 
      WHERE  employee_id < 102; 
BEGIN 
    FOR i IN c1 LOOP 
        basic := i.salary; 

        da := i.salary * ( 0.3 ); 

        hra := i.salary * ( .10 ); 

        IF ( i.salary < 4000 ) THEN 
          pf := i.salary * ( 0.07 ); 
        ELSIF( i.salary >= 4000 
               AND i.salary <= 24000 ) THEN 
          pf := i.salary * ( .16 ); 
        ELSIF SQL%NOTFOUND THEN 
          dbms_output.Put_line('SQL DATA NOT FOUND'); 
        END IF; 

        net := da + hra + basic - pf; 

        exit WHEN SQL%NOTFOUND; 

        dbms_output.Put_line(net); 

        --dbms_output.put_line(basic); 
        dbms_output.Put_line(i.last_name); 
    END LOOP; 
END; 
Re: net salary calculation [message #651151 is a reply to message #651125] Thu, 12 May 2016 03:15 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
As Michel already pointed out SQL%NOTFOUND can't be true for a FOR loop. Also into is completely pointless in a cursor declartion. And finally all those calculations can be done in one hit in the cursor.
DECLARE

  CURSOR c_emp_net IS
  SELECT last_name,
         salary 
         + (salary * 0.3) 
         + (salary * 0.1) 
         - (CASE WHEN i.salary < 4000 THEN salary *(0.07)
                 WHEN salary >= 4000 AND i.salary <= 24000) THEN salary * (0.16)
                 ELSE 0
            END) AS net
  FROM employees WHERE employee_id < 102;

BEGIN

  FOR i IN c_emp_net LOOP

    dbms_output.put_line(i.net);
    dbms_output.put_line(i.last_name);

  END LOOP;
  
END
;


You need to think about what happens if salary is > 24000.
Previous Topic: Hierarchy query help
Next Topic: create dummy rows
Goto Forum:
  


Current Time: Thu Apr 18 01:50:33 CDT 2024