–游标法
–student 有 sno sname ssex
–student_test 有 sno name sex
–SQL%NOTFOUND 是一个布尔值。与最近的sql语句(update,insert,delete,select)发生交互,当最近的一条sql语句没有涉及任何行的时候,则返回true。否则返回false。这样的语句在实际应用中,是非常有用的。例如要update一行数据时,如果没有找到,就可以作相应操作。
当update sno为5的这行记录,如果update影响条数为0,则插入一条数据。
–需求把student_test 表数据更新到student表
declare
cursor cur_pricetable is select * from student_test;–定义游标
begin
for v_pricetable in cur_pricetable
loop
update student set sname=v_pricetable.name,ssex=v_pricetable.sex where sno = v_pricetable.sno;
if sql%notfound then
insert into student(sno,sname,ssex) VALUES (v_pricetable.sno,v_pricetable.name,v_pricetable.sex);
end if;
end loop;
end ;