Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

sql - PROCEDURES using HR database

I want to Write a PROCEDURE that will first print the Employee Number and Salary of an employee (i.e. 7839). Then it will increase the salary of an employee 7839 (this will be employee number in the table employee) as per following conditions:

Condition-1: If experience is more than 10 years, increase salary by 20%.
Condition-2: If experience is greater than 5 years, increase salary by 10%.
Condition-3: All others will get an increase of 5% in the salary.

The program will print the Employee Number and salary before and after the increase i tried the following steps but not sure how accurate is it..I need to convert it to a PROCEDURE code.

please advise

    DECLARE
     veno  emp.empno%type:=&veno;
     vsal  emp.sal%type;
     vexp  number;
BEGIN
    select empno,sal,trunc(to_char(months_between(sysdate,hiredate)/12))into veno,vsal,vexp from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('before update:' ||chr(10)||veno||chr(10)||vsal);
    if vexp>=10 then
        update emp set sal=sal+(sal*.20) where empno=veno; 
        select sal into vsal from emp where empno=veno;
        DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
   elsif vexp>=5 then
        update emp set sal=sal+(sal*.10) where empno=veno;
        select sal into vsal from emp where empno=veno;
        DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
     else 
        update emp set sal=sal+(sal*.05) where empno=veno;
        select sal into vsal from emp where empno=veno;
        DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
   end if;
END;
/
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this one else go with RETURNING clause and add an exception block

DECLARE
     veno  emp.empno%type:=&veno;
     vsal  emp.sal%type;
     vexp  number;
BEGIN
    select empno,sal,trunc(to_char(months_between(sysdate,hiredate)/12))into veno,vsal,vexp from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('before update:' ||chr(10)||veno||chr(10)||vsal);
    if vexp>=10 then
        update emp set sal=sal+(sal*.20) where empno=veno; 
        select sal into vsal from emp where empno=veno;
        DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
   elsif vexp>=5 then
        update emp set sal=sal+(sal*.10) where empno=veno;
        select sal into vsal from emp where empno=veno;
        DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
     else 
        update emp set sal=sal+(sal*.05) where empno=veno;
        select sal into vsal from emp where empno=veno;
        DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
   end if;
END;
/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...