-4

1.这段代码到底是做什么的?
2.什么是数字(2)?

cursor c1 is    
select employee_id, department_id, commission_pct  from hr.employees; 
emprec c1%rowtype; 
v_hike  number(2);
begin 
open c1; 
loop 
   fetch c1 into emprec; 
   exit when c1%notfound;
   if  emprec.department_id = 40 then   v_hike := 10; 
   elsif emprec.department_id = 70 then   v_hike := 15;     
   elsif emprec.commission_pct > 0.30 then   v_hike := 5;
   else   v_hike := 10; 
end if; 


update employees set salary = salary + salary * v_hike/100  
where employee_id = emprec.employee_id; 
end loop; 
end;
4

2 回答 2

0

这段代码到底是做什么的?

首先,在 Oracle PL/SQL 中,它在语法上是不正确的,因为您错过了 PL/SQL 匿名块中的 DECLARE 关键字。你会得到一个编译错误。

让我一步一步解释:

cursor c1 is    
select employee_id, department_id, commission_pct  from hr.employees; 
emprec c1%rowtype; 

它是一个显式游标,它是一个工作区,用于存储选择查询的处理信息。emprec 是一种集合类型,当您发出 FETCH 语句时将保存结果集。

v_hike number(2);

这是一个声明为 NUMBER 数据类型的变量。NUMBER(2) 表示它可以保存 PRECISION 2 的数值。

begin 
open c1; 
loop 
   fetch c1 into emprec; 
   exit when c1%notfound;
   if  emprec.department_id = 40 then   v_hike := 10; 
   elsif emprec.department_id = 70 then   v_hike := 15;     
   elsif emprec.commission_pct > 0.30 then   v_hike := 5;
   else   v_hike := 10; 
end if; 


update employees set salary = salary + salary * v_hike/100  
where employee_id = emprec.employee_id; 
end loop; 
end;

接下来的步骤是,

  • 一个。打开光标

    湾。对于游标中的每一行,循环并准备 emprec 中的集合

    C。当没有要从游标中获取的记录时,然后退出

    d。根据 IF-ELSE 构造为 v_hike 变量分配一个数值。

    e. 执行 UPDATE 语句。

    F。走出循环。

    G。块的范围以 END 关键字结尾。

于 2014-09-08T11:34:39.410 回答
0

它是 oracle 游标定义。那段 sql 到底在做什么,很难知道你能读懂它有多难。

在第一行中,您可以看到 select which 适用于多组行。然后您定义了局部变量。v_hike。在循环中,它正在执行一些表达式,例如 count v_hike,然后为雇主设置新工资

number(2) 写错了,应该是: number(p,s) 其中小数点前的 ps 位和小数点后的 s 位..

于 2014-09-08T11:00:54.233 回答