Oracle 存储过程

来源:互联网 发布:mac屏幕共享连接失败 编辑:程序博客网 时间:2024/06/16 02:17

一,基本入门介绍

CREATE [OR REPLACE] PROCEDURE procedure_name
    [ (parameter [,parameter]) ]
IS
    [declaration_section]
BEGIN
    executable_section
[EXCEPTION
    exception_section]
END [procedure_name];

先初始化数据。我用的是Toad工具,下面的代码是在SQL Editor 中执行的。

create table students

  ID int,
  userName varchar(100),
  userPass varchar(100),
  userAge  int
)

insert into students values(1,'jack','jjjaa',23);
insert into students values(2,'rose','jjjaa',21);
insert into students values(3,'lucy','jjjaa',22);
insert into students values(4,'Tony','jjjaa',24);
commit;

当然,新建存储过程是需要在Procedure Editor中编写,但是执行存储过程又需要在SQL Editor中去执行,Procedure中是不可以执行(like exec的语句)的。

这里我们新建一个存储过程,对于某个用户添加年龄,哈哈,当然这个是没什么意义的,学习,从简单入手。在实际开发中,语法,原理是一样的。

create or replace procedure  SP_Update_Age
(
 uName in varchar,--note,here don't have length ,sql have lenth ,not in oracle.
 Age in int
)
as
begin
    update students set UserAge = UserAge + Age where userName = uName;
    commit;
end SP_Update_Age;

在执行存储过程之前,我们先查看原来的数据。

select * from students

/*********************


ID    USERNAME    USERPASS    USERAGE
   jack            jjjaa        23
   rose            jjjaa        21
   lucy            jjjaa        22

   Tony            jjjaa        24


**********************/

然后我们在SQL Editor中执行如下存储过程。注意看是怎么调用的:

exec SP_UPDATE_AGE('jack',1);
执行之后,查看数据,
select * from students;


/********************

ID    USERNAME    USERPASS    USERAGE
   jack    jjjaa    24  --noted,have changed 
   rose    jjjaa    21
   lucy    jjjaa    22
   Tony    jjjaa    24

*********************/

二,基本语法介绍

可以看出,基本的功能实现,调用完成。

下面,来看看基本语法:

1,变量赋值

 变量名 := 值;

2,判断语句。

if

比较式

then

begin

end;

end

if

结合起来写个简单例子:

create or replace procedure Test(x in out number)
is
begin
     if x<0 then
         begin
             x:= 0 - x;
        end;
     elsif x > 0 then     --noted here elsif 
         begin
             x:= x ;
        end;
     else
        x:=    0;
     end if;
end Test;


Test:

set serveroutput on;  --没这句话,看不到dmbs_output信息。
declare
       num number;
begin
    num:= -1;
    test(num);
    dbms_output.put_line( 'num = ' || num );
end;
/******************************
num = 1
PL/SQL procedure successfully completed.
*******************************/

3,For循环,

For  in ..loop;

set serveroutput on;
DECLARE
   x NUMBER := 100;
BEGIN
   FOR i IN 1..10 LOOP  --noted here 
      IF MOD(i,2) = 0 THEN     -- i is even
          dbms_output.put_line( 'i: '||i||' is even ' );
      ELSE
          dbms_output.put_line('i: '|| i||' is odd' );
      END IF;
      x := x + 100;
      dbms_output.put_line('x value: '|| x);
   END LOOP;
   COMMIT;
END;


/*************************
i: 1 is odd
x value: 200
i: 2 is even 
x value: 300
i: 3 is odd
x value: 400
i: 4 is even 
x value: 500
i: 5 is odd
x value: 600
i: 6 is even 
x value: 700
i: 7 is odd
x value: 800
i: 8 is even 
x value: 900
i: 9 is odd
x value: 1000
i: 10 is even 
x value: 1100
PL/SQL procedure successfully completed.




*************************/

后面再说遍历什么游标啊,数组啊。先从简单的 开始。

4,While 循环。

create or replace Procedure Test2(i in out number)
as
begin
     while i < 10 loop
            begin
                   i:= i+1;
           end;
          end loop;
end Test2;

来测试下。

set serveroutput on;
declare
       num number;
begin
    num:= 1;
    test2(num);
    dbms_output.put_line( 'num = ' || num );
end;


/*********************


num = 10
PL/SQL procedure successfully completed.


***********************/



原创粉丝点击