Oracle存储过程包含三部分:过程声明,执行过程部分,存储过程异常。
Oracle存储过程可以有无参数存储过程和带参数存储过程。
一、无参程序过程语法
1 create or replace procedure NoParPro
2 as ;
3 begin
4 ;
5 exception //存储过程异常
6 ;
7 end;
8
二、带参存储过程实例
1 create or replace procedure queryempname(pno%type) as
2 ame%type;
3 sjob emp.job%type;
4 begin
5 ....
7 exception
....
14 end;
15
三、 带参数存储过程含赋值方式 1 create or replace procedure runbyparmeters (isal in emp.sal%type,
sname out varchar,sjob in out varchar)
2 as icount number;
3 begin
4 select count(*) into icount from emp where sal>isal and job=sjob;
5 if icount=1 then
6 ....
9 else
10 ....
12 end if;
13 exception
14 when too_many_rows then
15 DBMS_OUTPUT.PUT_LINE('返回值多于1行');
16 when others then
17 DBMS_OUTPUT.PUT_LINE('在RUNBYPARMETERS过程中出错!');
18 end;
19
四、在Oracle中对存储过程的调用
过程调用方式一
1 declare
2 realsal emp.sal%type;
3 realname varchar(40);
4 realjob varchar(40);
5 begin //存储过程调用开始
6 realsal:=1100;
7 realname:='';
8 realjob:='CLERK';
9 runbyparmeters(realsal,realname,realjob); --必须按顺序
10 DBMS_OUTPUT.PUT_LINE(REALNAME||' '||REALJOB);
11 END; //过程调用结束
12
过程调用方式二
1 declare
2 realsal emp.sal%type;
3 realname varchar(40);
4 realjob varchar(40);
5 begin //过程调用开始
6 realsal:=1100;
7 realname:='';
8 realjob:='CLERK';
9 runbyparmeters(sname=>realname,isal=>realsal,sjob=>realjob); --指定值对应变量顺序可变
10 DBMS_OUTPUT.PUT_LINE(REALNAME||' '||REALJOB);
11 END; //过程调用结束sql存储过程实例
12
****************************************************
create or replace procedure test
as
begin
......
...
end;
***************************************************
create or replace PROCEDURE sp_test
(para1 in number,
result out integer)
as
testnum number;
begin
testnum:= para1*6;
result:=1;
exception
when others then
result:=-1;
end
******************************************************
在sql*plus里面运行类似的语句创建
CREATE OR REPLACE PROCEDURE Procdemo(col1 IN varchar2,col2 out varchar2 ) AS
v_String1 VARCHAR2(50) := 'Hello World!';
BEGIN
col2 := col1||'is col2';
DBMS_OUTPUT.PUT_LINE(col1);
END Procdemo;
/
***************************************************
SQL> create or replace procedure testpr
2 as
3
4 begin
5 dbms_output.put_line('Hello procedure.');
6 end;
7 /
Procedure created.
Elapsed: 00:00:01.92
SQL> commit;
Commit complete.
Elapsed: 00:00:00.20
SQL> set serveroutput on
SQL> exec testpr
Hello procedure.
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.40
********************************************************
CREATE PROCEDURE SP_TEST @BH CHAR(8) AS
/*
输入BH取得数据集;
*/
SELECT
Number,Name,Nation FROM Workers
WHERE
Number=@BH
Order by Number
GO
以上为一个普通的存储过程的写法.
**************************************************
create proc del @i int
as
delete from table1 where id=@i
运行存储过程
exec del
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论