1、假设student表如下:
    create table student(stuid int,stuname varchar(10),sex char(2));
2、定一个插入数据的过程如下:
    create or replace Procedure InserStudent(NewStudent in Student%Rowtype) as
iRec Integer;
Not_Exists_Student Exception;
    Begin
Select count(*) into iRec from student where stuId=newstudent.stuID;
if iRec>0 then
           Raise Not_Exists_Student;
else
Insert into Student values(newStudent.stuId,newStudent.stuName,newStudent.Sex);
commit;
end if;
    Exception
When Not_Exists_Student then
DBMS_output.put_line('要插入的编号为'||newStudent.stuId||'已存在');
When others then
DBMS_output.put_line('查询过程中发生意外情况');
    End InserStudent;3、问题:如何执行这个过程?我不知道参数部分怎么写,
   exec insert_student(这里面应该填什么?);

解决方案 »

  1.   

    SQL> declare n_student Student%RowType ;
      2  begin
      3  n_student.stuid := 55;
      4  n_student.stuname := 'test';
      5  n_student.sex := '1';
      6  InserStudent(n_student);
      7  end;
      8  /PL/SQL procedure successfully completed
      

  2.   

    你的过程存在问题Select count(*) into iRec from student where stuId=newstudent.stuID;
    ======〉
    如果没有数据,则会跳到exception块,错误是NO_DATA_FOUND,执行when others then
    实际上是不对的
      

  3.   

    如果没有数据的话,count(*) 会返回0值,不会跳到异常处理的