CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Oracle >  开发

请问,编写一个PL/SQL程序时,是不是要将Exception语句块放到程序的最后?

楼主RUBY_uu()2006-03-08 20:52:10 在 Oracle / 开发 提问

RT。  
  谢谢!! 问题点数:10、回复次数:4Top

1 楼cenlmmx(学海无涯苦作舟)回复于 2006-03-08 21:01:22 得分 2

不一定,如  
  declare  
  ......  
  begin  
  begin  
  insert   into   test(c1,c2)   value(dd,ff);  
  exception  
  when   others   then  
  ...........  
  end;  
  update   .................  
  exception  
  when   others   then  
  ................  
  end   ;Top

2 楼RUBY_uu()回复于 2006-03-08 21:24:13 得分 0

可是,为什么我下面的这个程序有错误:  
  create   or   replace   procedure   NO_new_stu(stu_name   in   student.stuname%type,  
  stu_id   out   student.stuid%type)  
  as  
  num   char(4);  
  s_id   char(4);  
  begin  
  select   max(stuid)   into   num   from   student;  
  select   stuid   into   s_id   from   student   where   stuname=stu_name;  
  if   s_id   is   null   then  
  if   substr(num,3,2)   between   '01'   and   '09'   then  
  stu_id:='1000'+substr(num,4,1)+1;  
  else  
  stu_id:='1000'+substr(num,3,2)+1;  
  end   if;  
  else  
  RAISE_APPLICATION_ERROR(-20001,'The   student   is   existed!');  
  end   if;  
  exception  
  when   others   then   dbms_output.put_line(SQLERRM);  
  end;  
  update   student   set   stuid=stu_id   where   stu_name=stuname;  
  end;  
  /  
   
  警告:   创建的过程带有编译错误。  
   
  SQL>   show   err  
  PROCEDURE   NO_NEW_STU   出现错误:  
   
  LINE/COL   ERROR  
  --------   --------------------------------  
  20/1           PLS-00103:   出现符号   "UPDATE"  
   
   
  而且,如果我把update语句放到when   others   then   dbms_output.put_line(SQLERRM);下面的话:  
   
  SQL>   create   or   replace   procedure   NO_new_stu(stu_name   in   student.stuname%type,  
      2     stu_id   out   student.stuid%type)  
      3     as  
      4     num   char(4);  
      5     s_id   char(4);  
      6     begin  
      7     select   max(stuid)   into   num   from   student;  
      8     select   stuid   into   s_id   from   student   where   stuname=stu_name;  
      9     if   s_id   is   null   then  
    10     if   substr(num,3,2)   between   '01'   and   '09'   then  
    11     stu_id:='1000'+substr(num,4,1)+1;  
    12     else  
    13     stu_id:='1000'+substr(num,3,2)+1;  
    14     end   if;  
    15     else  
    16     RAISE_APPLICATION_ERROR(-20001,'The   student   is   existed!');  
    17     end   if;  
    18     exception  
    19     when   others   then   dbms_output.put_line(SQLERRM);  
    20     update   student   set   stuid=stu_id   where   stu_name=stuname;  
    21     end;  
    22     /  
   
  过程已创建。  
   
  update这句干脆就不执行了,表student没有任何变化。  
   
  只有把update这句放到Exception的上方才对表进行了更改:  
  create   or   replace   procedure   NO_new_stu(stu_name   in   student.stuname%type,  
  stu_id   out   student.stuid%type)  
  as  
  num   char(4);  
  s_id   char(4);  
  begin  
  select   max(stuid)   into   num   from   student;  
  select   stuid   into   s_id   from   student   where   stuname=stu_name;  
  if   s_id   is   null   then  
  if   substr(num,3,2)   between   '01'   and   '09'   then  
  stu_id:='1000'+substr(num,4,1)+1;  
  else  
  stu_id:='1000'+substr(num,3,2)+1;  
  end   if;  
  else  
  RAISE_APPLICATION_ERROR(-20001,'The   student   is   existed!');  
  end   if;  
  update   student   set   stuid=stu_id   where   stu_name=stuname;  
  exception  
  when   others   then   dbms_output.put_line(SQLERRM);  
  end;  
  /  
   
  这是怎么回事呢??Top

3 楼sbaz(万神渡劫)回复于 2006-03-09 09:35:44 得分 4

create   or   replace   procedure   NO_new_stu(stu_name   in   student.stuname%type,  
  stu_id   out   student.stuid%type)  
  as  
  num   char(4);  
  s_id   char(4);  
  begin  
  begin  
  select   max(stuid)   into   num   from   student;  
  select   stuid   into   s_id   from   student   where   stuname=stu_name;  
  if   s_id   is   null   then  
  if   substr(num,3,2)   between   '01'   and   '09'   then  
  stu_id:='1000'+substr(num,4,1)+1;  
  else  
  stu_id:='1000'+substr(num,3,2)+1;  
  end   if;  
  else  
  RAISE_APPLICATION_ERROR(-20001,'The   student   is   existed!');  
  end   if;  
  exception  
  when   others   then   dbms_output.put_line(SQLERRM);  
  end;  
  update   student   set   stuid=stu_id   where   stu_name=stuname;  
  end;  
  /  
   
  少了个BEGIN.  
  有BEGIN就有END.  
  Top

4 楼mosquitoxh((IT,中间件,架构)http://it.paiming.org/bbs)回复于 2006-03-09 09:36:03 得分 4

begin  
            ................  
            begin  
                            ...............  
            exception  
            end;  
            ..................  
            begin  
                            ...............  
            exception  
            end;  
   
  exception  
            begin  
                  ......  
            exception  
            end  
  end;  
   
   
  就是这样一个原则  
   
  一个begin和end   之间可以有一个exceptionTop

相关问题

  • sql语句编写??
  • sql语句编写问题!
  • 编写完了SQL语句,关于……
  • 如何编写该sql语句?? 谢谢
  • 请问怎么用sql语句编写
  • 高难度sql 查询语句编写
  • PL/SQL 有switch语句吗?
  • 求一条PL/SQL语句
  • MS SQL SERVER 语句 转化为 PL*SQL语句 的问题,急!!!!!
  • 多表求记录总和sql语句编写?

关键词

  • dbms
  • application
  • stu
  • stuid
  • stuname
  • student
  • exceptionwhen
  • sqlerrm
  • 程序
  • asnum

得分解答快速导航

  • 帖主:RUBY_uu
  • cenlmmx
  • sbaz
  • mosquitoxh

相关链接

  • Oracle类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo