pl/sql 存储过程(procedure)
格式:
declare --(可选项,用于声明变量)
……
begin
……
exception --(异常处理)
……
end
/--运行
pl/sql要运行输出语句的话,需要先
set serveroutput on;
实例1(打印hello world).
declare
2 x varchar2(20);
3 begin
4 x := 'hello world';
5 dbms_output.put_line('x的值为:'||x);
6 end;
7 /
运行结果:
x的值为:hello world
PL/SQL procedure successfully completed
实例2(简单for循环)
set serveroutput on;
begin
for i in 1..5 loop
DBMS_output.put_line('i='||i);
end loop;
end;
/
实例3(if elsif else)
declare
x number;
begin
x := 0;
if x>0 then
dbms_output.put_line(x||'大于0');
elsif x<0 then
dbms_output.put_line(x||'小于0');
else
dbms_output.put_line(x||'等于0');
end if;
end;
/
运行结果: 0等于0
PL/SQL procedure successfully completed
实例4(goto的应用)
declare
x number;
begin
x := 0;
<<a_loop>>
x := x+1;
dbms_output.put_line('x= '||x);
if x<3 then
goto a_loop;
end if;
end;
/
运行结果:
x= 1
x= 2
x= 3
PL/SQL procedure successfully completed
实例5(自定义异常并且抛出)
declare
test varchar2(20);
e excetpion;
begin
select wtdempname into test from westemp where wtdempid= 6;
if test <> 'KING' then
raise e;
end if;
dbms_output.put_line('wtdempname= '||test);
exception
when e then
dbms_output.put_line('报错了..');
end;
/
实例6(特殊数据类型--记录 的定义1及应用)
set serveroutput on;
declare
myrecord westemp%rowtype;
begin
select * into myrecord from westemp where wtdempid= 6;
dbms_output.put_line('wtdempname= '||myrecord.wtdempname);
end;
/
运行结果:
wtdempname= f
PL/SQL procedure successfully completed
实例7(特殊数据类型--记录 的定义2及应用)
set serveroutput on;
declare
type myrecord is record(
id number(5),
name varchar2(20)
);
test_record myrecord;
begin
select wtdempid,wtdempname into test_record from westemp where wtdempid=6;
dbms_output.put_line('wtdempid= '||test_record.id||', wtdempname= '||test_record.name);
end;
/
运行结果:
wtdempid= 6, wtdempname= f
PL/SQL procedure successfully completed
实例8(显示游标的定义及应用)
SQL> set serveroutput on;
SQL> declare
2 cursor mycur is select * from westemp where wtddeptid=4;
3 myrecord westemp%rowtype;
4 begin
5 open mycur;
6 fetch mycur into myrecord;
7 while mycur%found loop
8 dbms_output.put_line('wtdempid= '||myrecord.wtdempid||', wtdempname= '||myrecord.wtdempname||', wtddeptid= '||myrecord.wtddeptid);
9 fetch mycur into myrecord;
10 end loop;
11 close mycur;
12 end;
13 /
运行结果:
wtdempid= 15, wtdempname= o, wtddeptid= 4
wtdempid= 3, wtdempname= c, wtddeptid= 4
wtdempid= 7, wtdempname= g, wtddeptid= 4
wtdempid= 12, wtdempname= l, wtddeptid= 4
PL/SQL procedure successfully completed
注意:游标的常用属性有:%found,%isopen,%notfound,%rowcount。
实例9 (带参数的显示游标和隐式游标)
SQL> set serveroutput on;
SQL> declare
2 cursor cur_param(no number) is
3 select wtdempname from westemp where wtddeptid=no;
4 begin
5 dbms_output.put_line('result set is ');
6 for cur in cur_param(4) loop
7 dbms_output.put_line('wtdempname= '||cur.wtdempname);
8 end loop;
9 end;
10 /
运行结果:
result set is
wtdempname= o
wtdempname= c
wtdempname= g
wtdempname= l
PL/SQL procedure successfully completed
实例10 (隐式游标)
隐式游标不需要在declare块中先是声明定义,也不需要手动打开和关闭,一个简单的隐式游标的例子如下:
SQL> begin
2 for cur in (select wtdempname from westemp) loop --此处的cur就是隐式游标
3 dbms_output.put_line('wtdempname= '||cur.wtdempname);
4 end loop;
5 end;
6 /
运行结果:
wtdempname= o
wtdempname= a
wtdempname= b
wtdempname= c
wtdempname= d
wtdempname= e
wtdempname= f
wtdempname= g
wtdempname= h
wtdempname= i
wtdempname= j
wtdempname= k
wtdempname= l
wtdempname= m
wtdempname= n
PL/SQL procedure successfully completed
实例11 (存储过程的创建和调用)
创建
SQL> create or replace procedure westProc(no in number,name out varchar2)
2 is
3 begin
4 select wtdempname into name from westemp where wtdempid=no ;
5 end;
6 /
Procedure created
调用
SQL> set serveroutput on;
SQL> declare
2 no number;
3 name varchar2(20);
4 begin
5 westProc(6,name);
6 dbms_output.put_line('name= '||name);
7 end;
8 /
name= f
PL/SQL procedure successfully completed
执行存储过程:
存储过程的调用有以下三种方法:
a.在PL/SQL中调用,如:
begin
myProc(7900);
end;
/
b.使用call来调用,如:
call myProc(7900);
c.通过execute调用,如:
execute myProc(7900);
注意:execute调用存储过程时,要么不带任何参数,要么只能是输入参数。
你的能量超乎你想象_________