当前位置:文档之家› PLSQL程序设计

PLSQL程序设计



例一
DECLARE tin_rec tin % rowtype ; v_passwd userlog.passwd % type ; errps EXCEPTION ;
BEGIN select * into tin_rec from tin ; select passwd into v_passwd from userlog
WHILE e LOOP
END LOOP ;
FOR i IN 1..10 LOOP
END IF;
END LOOP ;
EXIT [WHEN e] ;例外EXCEPTION WHEN 例外名1 THEN
… WHEN 例外名2 THEN
…… WHEN OTHERS THEN
…… END ;
系统预定义例外
begin
例三
For r1 in c1 loop
i := 0 ; for r2 in c2(r1.kno) loop
i := i + 1 ; if i=1 then
insert into tout values(r2.kno,r2.pno,r2.pdate); l_d:=r2.pdate; elsif r2.pdate = l_d then insert into tout values(r2.kno, r2.pno,r2.pdate); else exit; end if ; end loop; end loop ;
例三
Declare cursor c1 is select distinct kno from clog; cursor c2 (kindno char(3)) is select * from clog where kno=kindno order by day desc ; i number ; l_d date ;
open c1(v_depno) ;
例三
例题: 收发室收文件。文件分类,每类中文件依收到 日期先后顺序编号。值班人员收到文件后,要 在系统中登录文件类别,编号,收到日期。
(注:一天中收到的文件可能不止一份) 编一PL/SQL 程序,查询每一类中最后一天收
到的文件) 表clog(kno 类别 , pno 编号 , pdate 收发日期) 练习要点: 光标FOR循环,带参数的光标
where userid = tin_rec.ud ;
例一
if tin_rec.ps = v_passwd then insert into tout values(`login ok` );
else raise errps ;
end if ; exception when errps then
insert into tout values(`password error`〕; when no_data_found then insert into tout values(`userid error’〕; end;
例二
Close c1; insert into msg values(emp_num , s_sal) ; commit ; end;
光标for循环
For r in c1 loop
r.sal end loop;
如何打开光标 如何读光标区 如何判断读完 如何关闭光标
带参数光标
cursor c1 (depno char(3)) is select * from emp where dno=depno ;
显式光标
定义 delcare cursor c1 is select …
打开 open c1 读 fetch c1 into 变量/记录 close c1
显式光标
读取状态信息 c1%found c1%rowcound
用光标定义记录 temp c1%rowtype
例二
问题:
为职工长工资。从最低工资长,工资总额限 制在50万元。 Declare
二、PL/SQL程序设计
什么是PL/SQL
对SQL的扩充 过程化 后台技术
PL/SQL 程序结构
Declare begin
exception end /
变量类型
v_ename char(10) ; married boolean :=true ; v_ename emp.ename %TYPE ;
dup_val_on_index no_data_found too_many_rows
Select * into …
用户自定义例外
例外名定义 ex1 exception ;
引起例外 raise ex1 ;
例一
用户注册检查
tin : Ud
ps
tout :
msg
userlog : Userid passwd
光标
Oracle 使用Private SQL Area的工作区 执行SQL语句,并保存语句执行结果和 相关的状态信息。
光标是一个PL/SQL结构,利用光标可以 命名这些工作区,并通过光标访问工作 区中的信息。
隐式光标
隐式光标名:SQL 访问: SQL%ROWCOUNT SQL%FOUND SQL%NOTFOUND
cursor c1 is select eno,sal from emp order by sal for update of sal ;
emp_num number :=0 ; s_sal number ; e_sal number ; e_eno char(3);
例二
Begin open c1; select sum(sal) into s_sal from emp ; while s_sal<500000 loop fetch c1 into e_eno,e_sal ; exit when c1%notfound ; update emp set sal=sal*1.1 wwhheerereceunrroe=net_oefnco1 ;; s_sal:=s_sal + e_sal*0.1; emp_num:=emp_num+1 ; end loop ;
emp_rec emp % ROWTYPE ;
变量赋值 an_sal :=mon_sal *12 + nvl(comm,0) ;
Select ename INTO v_ename from emp where eno=` e01 ` ;
程序控制结构
IF e1 THEN ELSIF e2 THEN ELSIF e3 THEN ELSE
相关主题