写一些关于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 $counter in $low .. $high loop
sql
end loop
3。游标
在pl/sql程序中定义的游标叫做显式游标。
a.显式游标的处理由四个部分组成:
cursor $cursorname is $query; --定义游标
open $cursorname; --打开游标
fetch $cursorname into $othervariable --把游标中的东西取出
close $cursorname --关闭游标
b.游标属性
%found 布尔型属性,当最近一次读纪录成功时,为true.
%nofound 失败时,为false.
%isopen
%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;
create or replace procedure register (
p_studentid in students.id%type,
p_department in classes.department%type,
p_course in classes.course%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 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器