您好,欢迎访问一九零五行业门户网

Oracle 中Return 和exit的区别

在oracle存储过程中, 使用return 时 ,如果执行到return语句,会跳出整个语句(如果是循环,会跳出整个循环),将不再执行, 也就是结束了整个存储过程。 下面就用一个例子来说明一下 ,这个存储过程是根据员工号,查出员工姓名,为了得到直接的效果,我在
在oracle存储过程中,使用return 时,如果执行到return语句,会跳出整个语句(如果是循环,会跳出整个循环),将不再执行,也就是结束了整个存储过程。
下面就用一个例子来说明一下 ,这个存储过程是根据员工号,查出员工姓名,为了得到直接的效果,我在特定的地方进行了语句的打印,写的有点简单,忘大家谅解:
create or replace procedure pro_emp1( v_eno in number, v_resultcode out number, v_resulterrinfo out varchar2) is iv_eno emp.empno%type ; iv_name emp.ename%type; begin iv_eno:=v_eno; ---- 把入参的变量赋给定义的变量 v_resultcode :=-1; begin select ename into iv_name from emp where empno=iv_eno; dbms_output.put_line('雇员名:'||iv_name); exception when others then v_resultcode:=sqlcode; v_resulterrinfo :='没有想要的雇员名 :' ||sqlerrm; return; end; begin dbms_output.put_line('执行到大begin 中的小begin 中了方法了'); end; dbms_output.put_line('执行到大begin的末尾了');end;
执行这条存储过程,查询不存在的员工,可以看到后面的beign语句以及打印语句,将不在执行:
sql> var v_resultcode number;sql> var v_resultinfo varchar2;sql> exec pro_emp1 (7789,:v_resultcode,:v_resultinfo); pl/sql procedure successfully completedv_resultcode---------100v_resultinfo---------没有想要的雇员名 :ora-01403: 未找到数据
输入可以查询到的员工号码:
sql> exec pro_emp1 (7788,:v_resultcode,:v_resultinfo); 雇员名:scott执行到大begin 中的小begin 中了方法了执行到大begin的末尾了 pl/sql procedure successfully completedv_resultcode----------1v_resultinfo---------
下面是从网上摘来的,有兴趣的人可以看看:
create or replace procedure test5(o_cellphone in varchar2) is v_cellphone cc_quiz_stat.cellphone %type; v_name cc_quiz_stat %rowtype; v_state cc_quiz_stat.state %type; begin declare cursor cur_cc is select * from cc_quiz_stat; cursor cur_jc(v_n varchar2) is select state from cc_quiz_stat; begin open cur_cc; loop fetch cur_cc into v_name; exit when cur_cc%notfound; open cur_jc(o_cellphone); loop fetch cur_jc into v_state; exit when cur_jc %notfound; if (o_cellphone = v_name.cellphone) then return; else dbms_output.put_line('手机号' || v_name.cellphone || '省份' || v_state); end if; end loop; close cur_jc; end loop; close cur_cc; end; end test5;
执行结果:
手机号18900000000省份全国 手机号18900000000省份南京 手机号18900000000省份天津 手机号18900000000省份 上海 手机号18900000000省份北京
create or replace procedure test5(o_cellphone in varchar2) is v_cellphone cc_quiz_stat.cellphone %type; v_name cc_quiz_stat %rowtype; v_state cc_quiz_stat.state %type; begin declare cursor cur_cc is select * from cc_quiz_stat; cursor cur_jc(v_n varchar2) is select state from cc_quiz_stat; begin open cur_cc; loop fetch cur_cc into v_name; exit when cur_cc%notfound; open cur_jc(o_cellphone); loop fetch cur_jc into v_state; exit when cur_jc %notfound; if (o_cellphone = v_name.cellphone) then exit; else dbms_output.put_line('手机号' || v_name.cellphone || '省份' || v_state); end if; end loop; close cur_jc; end loop; close cur_cc; end; end test5;
执行结果:
手机号18900000000省份全国 手机号18900000000省份南京 手机号18900000000省份天津 手机号18900000000省份 上海 手机号18900000000省份北京 手机号18900000002省份全国 手机号18900000002省份南京 手机号18900000002省份天津 手机号18900000002省份 上海 手机号18900000002省份北京 手机号18900000003省份全国 手机号18900000003省份南京 手机号18900000003省份天津 手机号18900000003省份 上海 手机号18900000003省份北京 手机号18900000004省份全国 手机号18900000004省份南京 手机号18900000004省份天津 手机号18900000004省份 上海 手机号18900000004省份北京
return 跳出整个循环,本循环后面的不再执行,
exit 跳出本次循环,下次继续执行本次循环
其它类似信息

推荐信息