对于学生选课表sc(sno,cno,grade),创建相同结构的新表sc1,并将sc数据全部加入到sc1,然后通过游标对sc1进行成绩更新:
成绩为: 80~100 更新为5;
60~80, 更新为3;
低于60分,更新为0;
更新之后对比sc和sc1,查看更新效果
```declare
sc1_sno sc1.sno%type;
sc1_cno sc1.cno%type;
sc1_grade sc1.grade%type;
cursor sc_cursor
is select sno,cno,grade from sc;
begin
open sc_cursor;
loop
fetch sc_cursor into sc1_sno,sc1_cno,sc1_grade;
exit when sc_cursor % notfound;
if sc1_grade>=80 then
sc1_grade:=5;
else
if sc1_grade<60 then
sc1_grade:=0;
else
sc1_grade:=3;
end if;
end if;
insert into sc1(sno,cno,grade) values (sc1_sno,sc1_cno,sc1_grade);
end loop;
close sc_cursor;
commit;
end;
错误报告:
ORA-00001: 违反唯一约束条件 (STUDENT.SYS_C004830)
ORA-06512: 在 line 21
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.