【问题标题】:PROCEDURES using HR database使用 HR 数据库的程序
【发布时间】:2015-08-21 09:38:16
【问题描述】:

我想编写一个程序,首先打印员工的员工编号和薪水(即 7839)。 然后它将增加员工 7839 的薪水(这将是员工 员工表中的编号)按照以下条件:

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.

程序会打印加薪前后的员工编号和工资 我尝试了以下步骤,但不确定它有多准确..我需要将其转换为 PROCEDURE 代码。

请指教

    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;
/

【问题讨论】:

  • 顺便说一句,where 条件在哪里?
  • 不知道怎么写
  • 给我们表名以及它们之间的关系如何?
  • 这是 HR 模式 .. 我会更新这个问题。谢谢

标签: sql plsql procedure


【解决方案1】:

用 RETURNING 子句试试这个,并添加一个异常块

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;
/

【讨论】:

  • 尝试在你的过程中实现同样的@Shak
猜你喜欢
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 2013-06-03
相关资源
最近更新 更多