Jump to content

Urgent Help Needed > what's wrong with these codes


OilSheikh

Recommended Posts

Help would be much appreciated.

 

1. User prompted for values... values checked ... values entered into table.

 

ACCEPT eno NUMBER -
PROMPT "Enter Employee no :"
ACCEPT enam VARCHAR2 -
PROMPT "Enter Employee name :"
ACCEPT jo VARCHAR2 -
PROMPT "Enter Job Title :"
ACCEPT man NUMBER -
PROMPT "Enter Manager no :"
ACCEPT hd DATE -
PROMPT "Enter Starting Date :"
ACCEPT sala NUMBER -
PROMPT "Enter Employee's salary :"
ACCEPT com NUMBER -
PROMPT "Enter Employee commission :"
ACCEPT dno NUMBER -
PROMPT "Enter Employee's Department number :"

EXECUTE add_any_emp ( 'eno','enam','jo','man','hd','sala','com','dno');


CREATE OR REPLACE PROCEDURE add_any_emp 
  (aEMPNO IN NUMBER, aENAME IN VARCHAR2, aJOB IN VARCHAR2, aMGR IN NUMBER, aHIREDATE IN DATE, aSAL IN NUMBER, aCOMM IN NUMBER, aDEPTNO IN NUMBER )

AS
BEGIN
SELECT EMPNO FROM EMP2;
IF EMP2.EMPNO = aEMPNO THEN
      raise_application_error(-20101, 'Employee exists');
  ELSE 
        IF aSAL > 20000 THEN
           aSAL = 20000
              ELSE
                    INSERT INTO EMP2
                        VALUES ( aEMPNO, aENAME, aJOB, aMGR, aHIREDATE, aSAL, aCOMM, aDEPTNO );
        END IF;
END IF;
end;

Link to comment
Share on other sites

2. Calculate average salary of all employees excl. the highest paid fat cat  :D

 

CREATE OR REPLACE FUNCTION average_emp
RETURN AVGSAL
IS
BEGIN
SELECT SAL
FROM EMP2
WHERE EMPNO != NULL
AND AVGSAL (NUMBER ) = AVG(SUM(SAL) - MAX(SAL)) ;

DBMS_OUTPUT.PUT_LINE('Average Salary is : ' || AVGSAL);

 

Link to comment
Share on other sites

In the second one, avg(Sum(sal) - max(sal)) looks suspicious.  sum() and max() are already aggregates that will give a single result, and you cannot take the average of a single result.  Instead you should either use a where clause to exclude the highest salary and then use avg(), or do the average calculation explicitly using sum() and max() and count().

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.