Ask the Expert

Creating a stored procedure with a function in it

I am trying to create a stored procedure that has a function in it. Unfortunately, I can't seem to be able to call the function within the procedure.

Here is the code.

 CREATE OR REPLACE PROCEDURE  New_Emp(p_EMP_NAME IN VARCHAR2, p_EMP_JOB IN VARCHAR2, p_DEPT_NO IN NUMBER) IS
    v_NEW_EMP_ID EMP.EMPNO%TYPE;
    v_HIGHEST_DEPTNO DEPT.DEPTNO%TYPE;
    v_Avg_min EMP2.SAL%TYPE;

      FUNCTION Avg_Emp RETURN REAL AS
      BEGIN
      SELECT ROUND(AVG(SAL)) - MIN(SAL)
        INTO v_Avg_min
    FROM EMP;
      RETURN NUMBER;
      END;

	BEGIN
     	 SELECT MAX(EMPNO) + 1
	        INTO v_NEW_EMP_ID
		   FROM EMP;
	
     	 SELECT MAX(DEPTNO)
	       INTO v_HIGHEST_DEPTNO
            FROM DEPT;

      INSERT INTO EMP2(EMPNO, ENAME, JOB, MGR, HIREDATE,                 
				   SAL, COMM, DEPTNO, STARS)
		      VALUES
			      (v_NEW_EMP_ID, p_EMP_NAME, p_EMP_JOB, 						7566, '20-DEC-2001', v_Avg_min, 						NULL, v_HIGHEST_DEPTNO, NULL); END;
/ 

What am I doing wrong?

    Requires Free Membership to View

In your function, please replace RETURN NUMBER statement with RETURN V_AVG_MIN and it should work.

This was first published in January 2005

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: