Home » SQL & PL/SQL » SQL & PL/SQL » Help with Function (Oracle )
Help with Function [message #649244] Fri, 18 March 2016 22:09 Go to next message
Nagaram
Messages: 2
Registered: February 2016
Junior Member
Hi All

Here is the requirement

---

Create a PL/SQL function called GET_ANNUAL_INCOME to return the annual compensation for the specified employee, computed from an employee's monthly salary and commission based on this formula:
annual_ income = salary*12 +(commission_pct*salary*12)
The GET_ANNUAL_INCOME function should accept one parameter employee id and using appropriate query against hr.employees table calculate the annual income for that employee.
--

Here is my script, but its throwing end of file error

CREATE TABLE Employee
(
EmpId INT,
salary int,
commission_pct int
);

INSERT INTO Employee VALUES(1,10000,10);
INSERT INTO Employee VALUES(2,20000,15);

CREATE OR REPLACE FUNCTION GET_ANNUAL_INCOME (iEmpId IN INT) 
RETURN FLOAT 
IS annual_income FLOAT;
BEGIN  
  SELECT salary *12 +(commission_pct*salary*12) 
  INTO annual_income
  FROM Employee 
  WHERE EmpId = iEmpId;
  
  RETURN(annual_income);
END;


Could you please help me with this.


[mod-edit: code tags added by bb; please add them yourself in the future]

[Updated on: Fri, 18 March 2016 22:44] by Moderator

Report message to a moderator

Re: Help with Function [message #649245 is a reply to message #649244] Fri, 18 March 2016 22:52 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9088
Registered: November 2002
Location: California, USA
Senior Member
Your code runs without error for me, as shown below. Please post a copy and paste of a run of your code, in the same manner that I have done below, including your Oracle version, line numbers, how you tested the function, and all error messages received.

SCOTT@orcl> SELECT banner FROM v$version
  2  /

BANNER
--------------------------------------------------------------------------------
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
PL/SQL Release 12.1.0.2.0 - Production
CORE	12.1.0.2.0	Production
TNS for 64-bit Windows: Version 12.1.0.2.0 - Production
NLSRTL Version 12.1.0.2.0 - Production

5 rows selected.

SCOTT@orcl> CREATE TABLE Employee
  2  (
  3  EmpId INT,
  4  salary int,
  5  commission_pct int
  6  );

Table created.

SCOTT@orcl> 
SCOTT@orcl> INSERT INTO Employee VALUES(1,10000,10);

1 row created.

SCOTT@orcl> INSERT INTO Employee VALUES(2,20000,15);

1 row created.

SCOTT@orcl> 
SCOTT@orcl> CREATE OR REPLACE FUNCTION GET_ANNUAL_INCOME (iEmpId IN INT)
  2  RETURN FLOAT
  3  IS annual_income FLOAT;
  4  BEGIN
  5    SELECT salary *12 +(commission_pct*salary*12)
  6    INTO annual_income
  7    FROM Employee
  8    WHERE EmpId = iEmpId;
  9  
 10    RETURN(annual_income);
 11  END;
 12  /

Function created.

SCOTT@orcl> SHOW ERRORS
No errors.
SCOTT@orcl> SELECT employee.*,
  2  	    get_annual_income (EmpId) AS annual_income
  3  FROM   employee
  4  /

     EMPID     SALARY COMMISSION_PCT ANNUAL_INCOME
---------- ---------- -------------- -------------
         1      10000             10       1320000
         2      20000             15       3840000

2 rows selected.

Re: Help with Function [message #649247 is a reply to message #649244] Sat, 19 March 2016 01:31 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
From your previous topic:

John Watson wrote on Thu, 04 February 2016 18:23
...
please be precise in future.
What is the effect when you use scope=both rather than scope=memory ?


Michel Cadot wrote on Thu, 04 February 2016 18:37

Quote:
please be precise in future.


And copy and paste your SQL*Plus session like John did instead of trying to explain (and fail).

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Also always post your Oracle version, with 4 decimals.


In the end, feedback and thank people who spent time to help you.

[Updated on: Sat, 19 March 2016 01:32]

Report message to a moderator

Re: Help with Function [message #649289 is a reply to message #649247] Mon, 21 March 2016 07:59 Go to previous message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
You will get an error if the passed employee id is not in the table. do the following

CREATE OR REPLACE FUNCTION GET_ANNUAL_INCOME (iEmpId IN INT) 
RETURN FLOAT 
IS annual_income FLOAT;
BEGIN  
  SELECT salary *12 +(commission_pct*salary*12) 
  INTO annual_income
  FROM Employee 
  WHERE EmpId = iEmpId;
  
  RETURN annual_income;
exception
when no_data_found then
  return 0;
END;
/
Previous Topic: Get all installments based on several plans (get unique values based on a single column)
Next Topic: saving the htp buffer to a file
Goto Forum:
  


Current Time: Fri Apr 19 14:44:36 CDT 2024