简体   繁体   中英

PL/SQL how to write multiple statement in a single stored procedure

I need to do the following problem,

Write a procedure that decreases the salary by a 10% for all employees who earn less than the current average salary. Treat the content of table EMP as “all employees”. Have your procedure print the name and salary of the first person whose salary is just below the average.

What should be the approach to solve the problem?

Write a procedure that decreases the salary by a 10% for all employees who earn less than the current average salary.

CREATE OR REPLACE PROCEDURE UPDATE_EMP IS
BEGIN
UPDATE EMP
SET SAL= SAL-(SAL*0.1) 
WHERE SAL<(SELECT AVG(SAL) FROM EMP);
END;

AND THE OTHER ONE : Have your procedure print the name and salary of the first person whose salary is just below the average.

SELECT e.ename
     , e.sal
 from 
    (select ename
            , sal 
      from emp 
      where sal < (select avg(sal)  
                     from emp
                   ) 
     order by sal desc 
     )e  
  where ROWNUM =1;

Now I need to connect both.. How could I do that ...

It sounds like you are a bit fuzzy on what a stored procedure is and how it can help you do complicated tasks involving many SQL statements.

You follow these directions on how a construct a stored procedure.

http://www.devshed.com/c/a/Oracle/Oracle-Stored-Procedures/

Stored procedures are wonderful structures that allow you to put multiple SQL statements into one structure, saving out variables for use in the next SQL statement. So all you have to do is invoke the stored procedure, and all the sql statements are run, and your answer is returned or table modification is committed.

You need something like this:

CREATE OR REPLACE PROCEDURE UPDATE_EMP RETURN name, value IS
BEGIN

    UPDATE EMP
    SET SAL= SAL-(SAL*0.1) 
    WHERE SAL<(SELECT AVG(SAL) FROM EMP);

    SELECT e.ename INTO name_to_return, e.sal INTO sal_to_return from 
    (select ename, sal from emp where sal < (select 
     avg(sal)from emp) order by sal desc)e   where ROWNUM =1;

    RETURN name_to_return, sal_to_return;
END;

The syntax may be a bit off, when you get it working, post your answer here as a new answer, and check mark it as the answer, and you are much more likely to get help like this in the future.

    CREATE OR REPLACE PROCEDURE DISPLAY_EMP IS
    IS
    CURSOR emp_cur
    IS 
    SELECT ename
          ,sal
    FROM  emp
    WHERE SAL<( SELECT AVG(SAL) FROM EMP)
    ORDER BY sal desc;
    v_emp_row emp_cur%ROWTYPE;

    BEGIN
    --update all the employee having sal less than avg sal

    UPDATE EMP
    SET SAL= SAL-(SAL*0.1) 
    WHERE SAL<(SELECT AVG(SAL) FROM EMP);

    --display all the employee having sal less than avg sal
     OPEN emp_cur ;
      LOOP
      FETCH emp_cur INTO v_emp_row;
      EXIT WHEN emp_cur%NOTFOUND;
      DBMS_OUTPUT.PUT_LINE('EMPLOYEE NAME : '||v_emp_row.ename||' '
                         ||'SALARY : '||v_emp_row.sal);
      END LOOP;
      CLOSE emp_cur;
    END DISPLAY_EMP;

   --call the display_emp proc to display all the emp
    BEGIN
     DISPLAY_EMP ;
    END;

您需要一个过程来执行更新,另一个函数或select语句用于打印薪水刚好低于平均值的第一个人的姓名和薪水。

CREATE OR REPLACE PROCEDURE DISPLAY_EMP IS IS CURSOR emp_cur IS SELECT ename ,sal FROM emp WHERE SAL<( SELECT AVG(SAL) FROM EMP) ORDER BY sal desc; v_emp_row emp_cur%ROWTYPE;

BEGIN
--update all the employee having sal less than avg sal

UPDATE EMP
SET SAL= SAL-(SAL*0.1) 
WHERE SAL<(SELECT AVG(SAL) FROM EMP);

--display all the employee having sal less than avg sal
 OPEN emp_cur ;
  LOOP
  FETCH emp_cur INTO v_emp_row;
  EXIT WHEN emp_cur%NOTFOUND;
  DBMS_OUTPUT.PUT_LINE('EMPLOYEE NAME : '||v_emp_row.ename||' '
                     ||'SALARY : '||v_emp_row.sal);
  END LOOP;
  CLOSE emp_cur;
END DISPLAY_EMP;

--call the display_emp proc to display all the emp BEGIN DISPLAY_EMP ; END;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM