选择显示字体大小

pl/sql的一些语法

写一些关于pl/sql的语法,免得等到用到的时候还要去乱翻。
1。控制流程(if,while)
2。循环(for)
3。游标(cursor)
4。异常处理(exception)

1。控制流程()


a.条件语句
if <statement> then
   pl/sql
end if;

if <statement> then
   pl/sql
else
   pl/sql
end if;

if
<statement> then
   pl/sql
elsif <statement> then
   pl/sql
end if;


2。循环

a.simple loop
loop
   sql
   exit when <statement>;
end loop;

loop
   sql
   if
<statement> then
   exit;
   end if;
end loop;

b.while loop
while <statement> loop
   sql
end loop;

c.for loop
for &#36;counter in &#36;low .. &#36;high loop
   sql
end loop


3。游标

在pl/sql程序中定义的游标叫做显式游标。

a.显式游标的处理由四个部分组成:
cursor &#36;cursorname is &#36;query;   --定义游标
open &#36;cursorname;         --打开游标
fetch &#36;cursorname into &#36;othervariable  --把游标中的东西取出
close &#36;cursorname    --关闭游标

b.游标属性
&#37;found         布尔型属性,当最近一次读纪录成功
,为true.
&#37;nofound                失败时,为false.
&#37;isopen
&#37;rowcount      返回已从游标中读取的记录数。

c.参数化游标

所有的sql语句在上下文区内部都是可执行的,因此都有一个游标指向上下文区,此游标就是所谓的sql游标。
与显式游标不同,sql游标不被程序打开和关闭。


4.异常处理概念

异常处理是用来处理正常执行过程中未预料的事件。如果pl/sql程序块一旦产生异常而又没有指出如何处理时,程序会自动终止。
异常处理部分放在pl/sql的后半部分,结构为:

exception
       when first_exception then <code to handle first exception>
       when second_exception then <code to handle second exception>
       when others then <
code to handle second exception>  --others必须放在最后

异常分为预定义和用户定义两种。
用户定义的异常是通过显式使用raise语句来引发。如

declare
  e_toomanystudents exception;  -- 类型为exception,用于指示错误条件
  v_currentstudents number(3);  -- his-101学生注册当前号
  v_maxstudents number(3);      -- his-101学生注册允许的最大号

begin
  /* 找出注册学生当前号和允许的最大号 */

  select current_students, max_students

    into v_currentstudents, v_maxstudents

    from classes

    where department = 'his' and course = 101;

  /* 检查学生的号 */

  if v_currentstudents > v_maxstudents then

/* 太多的学生注册,则触发例外处理 */

  raise e_toomanystudents;

  end if;

exception

  when e_toomanystudents then

    /* 当太多的学生注册,就插入信息解释发生过错误 */

    insert into log_table (info) values ('history 101 has ' v_currentstudents

      'students: max allowed is ' v_maxstudents);

end;


end;

用户定义的的异常处理
可以使用raise_application_error来创建自己的错误处理:
raise_application_error(error_number,error_message,[keep_errors]);
其中
error_number是从-20000到-20999之间的参数;error_message是相应的提示信息,小于512字节。如:

create or replace procedure register (
p_studentid in students.id&#37;type,
p_department in classes.department&#37;type,
p_course in classes.course&#37;type) as
v_currentstudents number;  -- 班上学生的当前号
v_maxstudents number;      -- 班上学生的最大号

begin
/* 找出学生的当前号和最大号 */
select current_students, max_students
 into v_currentstudents, v_maxstudents
from classes
where course = p_course
and department = p_department;

/* 确认另外的学生是否有足够的教室*/
if v_currentstudents + 1 > v_maxstudents then
raise_application_error(-20000, 'can''t add more students to '
p_department ' ' p_course);
end if;

/* 加一个学生在本班 */
classpackage.addstudent(p_studentid, p_department, p_course);

exception
when no_data_found then
   
raise_application_error(-20001, p_department ' ' p_course
         
' doesn''t exist!');
end register;


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons