Oracle plsql程序设计基础

  • 格式:pdf
  • 大小:351.51 KB
  • 文档页数:23

Oracle9i 开发指南:PL/SQL程序设计 清华大学出版社 ISBN 7-302-08002-x Ben整理 2004年 秋 MSN: mascotzhuang@hotmail.com

Oracle常用命令举例

󰁺 基本语法

󰂄 % type用法

󰂄 %rowtype用法

󰂄 TYPE用法

󰂄 游标的使用

󰂄 for 循环

󰂄 loop循环

󰂄 while循环

󰂄 if / else 的用法

󰂄 case 的用法

󰁺 错误定义

󰂄 error的设定

󰂄 exception用法

󰁺 存储过程及函数

󰂄 procedure 的建立和调用

󰂄 function的建立和调用

󰁺 参数的调用(in 模式为按址调用,out / in out模式为按值调用。NOCOPY 强行转换成按址调用)。

󰁺 软件包及封装

󰂄 软件包(PACKAGE)的建立和调用

󰂄 软件包的全局结构

󰂄 封装函数的纯度

󰁺 查看源代码及建立用户、用户的权限

󰂄 源代码的查看

󰂄 建立用户及登陆

󰂄 授予权限和权限收回

󰁺 依赖

󰂄 直接依赖

󰂄 查看依赖

󰂄 包之间调用

󰁺 触发器

󰂄 建立简单的触发器

󰂄 触发器分类

󰂄 稍复杂的触发器

󰂄 条件谓词

󰂄 触发器中不可使用Commit

󰂄 系统触发器举例(LOGON)

󰂄 instead of 触发器 Oracle9i 开发指南:PL/SQL程序设计 清华大学出版社 ISBN 7-302-08002-x Ben整理 2004年 秋 MSN: mascotzhuang@hotmail.com

1. % type用法,提取% type所在字段的类型 declare

myid dept.id % type;

myname dept.name % type;

begin

select id,name into myid,myname from dept;

dbms_output.put_line(myid);

dbms_output.put_line(myname);

end;

/

2. %rowtype用法,提取%rowtype所在的字段的类型 declare

type type_dept is table of dept % rowtype

index by binary_integer;

tb type_dept;

begin

tb(1).id:='001';

tb(2).id:='001';

dbms_output.put_line(tb.COUNT);

end;

/

3. TYPE用法,相当于结构体 declare

lv_order_date DAte:=sysdate;

lv_last_txt varchar2(5) default '001';

lv_last varchar2(10) not null:='us';

TYPE type_test is record(

myid dept.id % type,

myname dept.name % type);

rec type_test;

begin

lv_order_date:=sysdate;

dbms_output.put_line(lv_last);

select id,name into rec from dept;

dbms_output.put_line(rec.myid);

dbms_output.put_line(rec.myname);

end; Oracle9i 开发指南:PL/SQL程序设计 清华大学出版社 ISBN 7-302-08002-x Ben整理 2004年 秋 MSN: mascotzhuang@hotmail.com / 4. 游标的使用

declare

g_id char(10):='002';

find_not char(1):='N';

cursor cur is

select * from dept; TYPE type_dept is record( cur指向表

myid dept.id % type,

myname dept.name % type,

myaddr dept.addr % type);

rect type_dept;

begin

open cur;

loop

fetch cur into rect;

exit when cur% NOTFOUND; 提取cur指向的记录到rect结构中

if rect.myid=g_id then

find_not:='Y';

dbms_output.put_line('Find it!!');

dbms_output.put_line('DEPT ID:' || rect.myid);

dbms_output.put_line('NAME:' || rect.myname);

dbms_output.put_line('ADDR:' || rect.myaddr);

end if;

end loop;

close cur;

if find_not='N' then

dbms_output.put_line('no record');

end if;

end;

/

5. for 循环

begin

for i in 1..5 loop

dbms_output.put_line(i);

end loop;

end;

/

6. loop循环 Oracle9i 开发指南:PL/SQL程序设计 清华大学出版社 ISBN 7-302-08002-x Ben整理 2004年 秋 MSN: mascotzhuang@hotmail.com declare

v number:=1;

begin loop

dbms_output.put_line(v);

exit when v>5;

v:=v+1;

end loop;

end; /

7. while循环

declare

v number:=1;

begin

while v<5 loop

dbms_output.put_line(v);

v:=v+1;

end loop;

end;

/

8. error的设定

declare

v1 number:=90;

begin

if v1=10 then dbms_output.put_line('v1 is 10');

elsif v1=20 then dbms_output.put_line('v2 is 20');

else goto err;

dbms_output.put_line('normal end');

<>

dbms_output.put_line('error found');

end if;

end;

/

Oracle9i 开发指南:PL/SQL程序设计 清华大学出版社 ISBN 7-302-08002-x Ben整理 2004年 秋 MSN: mascotzhuang@hotmail.com

9. exception用法 declare

ex Exception;

begin

Update dept set name='Edison'

where id='100';

if SQL%NOTFOUND Then

Raise ex;

end if;

Exception

When ex then

dbms_output.put_line('update failed.');

end;

/

declare

type rc_dept is record (

myid dept.id%type,

myname dept.name%type,

myaddr dept.addr%type

);

tb rc_dept;

begin

select id,name,addr into tb from dept where id=:gb_id;

dbms_output.put_line('id:' || tb.myid);

dbms_output.put_line('name:' || tb.myname);

dbms_output.put_line('addr:' || tb.myaddr);

exception

when NO_DATA_FOUND then

dbms_output.put_line('no record is found');

when TOO_MANY_ROWS then

dbms_output.put_line('too many rows are selected');

when OTHERS then

dbms_output.put_line('undefine error');

dbms_output.put_line('error coede: ' || SQLCODE);

dbms_output.put_line('error message:' || SQLERRM);

end;

/