oracle.中PL/SQL的编程解答一个题目.!

来源:百度知道 编辑:UC知道 时间:2024/06/08 02:49:50
题目:
1.假设有两个表,职员表(工号,姓名,工资)和津贴表(工号,津贴)要求用游标完
成操作:取出职员表中工资大于1000元的记录,将其工资的30%作为津贴插入
津贴表中.

于是建了如下两个表:并插入测试数据;
create table Emplyoe(id number,name varchar2(50),sal number);
create table jingTie(id number,jinTie number);
alter table Emplyoe add constraint FK_Emplyoe primary key(id);
alter table jingTie add constraint FK_JingTie primary key(id);

insert into Emplyoe values(1,'张三',7800);
insert into Emplyoe values(2,'李四',900);
insert into Emplyoe values(3,'王五',1230);
insert into Emplyoe values(4,'找六',2000);

PL/SQL代码如下:

declare
--定义游标;
cursor c is select id,(SAL*0.3) SAL,SAL from Emplyoe for update;
id number;
sal number;
sal2 number;
begin
open c;
loop
fetch c into id,sal,sal2;

if sal2>=1000 then

把 exit when c%notfound; 放到fetch c into id,sal,sal2;之后。即:
fetch c into id,sal,sal2;
exit when c%notfound;

重复的原因是id,sal,sal2;保存了最后一条记录的值。

declare
--定义游标;
cursor c is select id,(SAL*0.3) sal,sal from Emplyoe for update;
id number;
sal number;
sal2 number;
BEGIN
open c;
loop
fetch c into id,sal,sal2;
exit when c%notfound;
if sal2>=1000 then
--insert into jingTie values(id,sal);
--dbms_output.put_line(' '||id||' '||sal);
dbms_output.put_line(' '||sal);

end if;
--退出;
--退出;
end loop;

close c;
end;