sql statement ignored and from key word not found

472 Views Asked by At

This results in an ORA-00923: From keyword not found error:

create or replace function gr_pay_hrly (p_wage_rate IN Number, 
                                        p_hrs_worked In number)
Return Number IS

  t_wage pay_history.wage_rate%type;
  t_hours pay_history.hrs_worked%type;
  t_gross_pay number(10,2);

BEGIN

  select wage_rate into t_wage, 
         hrs_worked into t_hours
    from pay_history
   where wage_rate = p_wage_rate
     and hrs_worked = p_hrs_worked;

  IF t_hours >= 40 THEN
    t_gross_pay := (((t_hours - 40) * t_wage) * 1.5);
  ELSE
    t_gross_pay := t_hours * t_wage;
  END IF;

  RETURN t_gross_pay;

  END;
  /
1

There are 1 best solutions below

0
On

Your issue is the INTO clause -- it's only declared once in a statement:

SELECT ph.wage_rate,
       ph.hrs_worked
  INTO t_wage, t_hours
  FROM PAY_HISTORY ph
 WHERE ph.wage_rate = p_wage_rate
   AND ph.hrs_worked = p_hrs_worked;

The population of variables is based on the order of the columns declared in the SELECT clause.

Full re-write:

If you subtract 40 from the hours_worked with is also 40, you'll just be multiplying by zero. And you want to add the straight time payout to the overtime...

CREATE OR REPLACE FUNCTION gr_pay_hrly (p_wage_rate IN NUMBER, 
                                        p_hrs_worked IN NUMBER)
RETURN NUMBER IS

  t_gross_pay NUMBER(10,2);

BEGIN

  SELECT CASE 
           WHEN ph.hrs_worked > 40 THEN ((ph.hrs_worked - (ph.hrs_worked - 40)) * ph.wage_rate) + (ph.hrs_worked - 40) * ph.wage_rate * 1.5
           WHEN ph.hrs_worked <= 40 THEN ph.hrs_worked * ph.wage_rate
           ELSE 0
         END
    INTO t_gross_pay
    FROM PAY_HISTORY ph
   WHERE ph.wage_rate = p_wage_rate
     AND ph.hrs_worked = p_hrs_worked;

  RETURN t_gross_pay;

END;