首页 > 代码库 > mysql与oracle的存储过程有什么区别?

mysql与oracle的存储过程有什么区别?

MySQL存储过程

 

(1). 格式

 

MySQL存储过程创建的格式:CREATE PROCEDURE过程名 ([过程参数[,...]])

[特性 ...]过程体

 

案例分析:

 

参数

 

MySQL存储过程的参数用在存储过程的定义,共有三种参数类型,IN,OUT,INOUT,形式如:

 

CREATE PROCEDURE([[IN |OUT |INOUT ] 参数名数据类形...])

 

IN 输入参数:表示该参数的值必须在调用存储过程时指定,在存储过程中修改该参数的值不能被返回,为默认值

 

OUT 输出参数:该值可在存储过程内部被改变,并可返回

 

INOUT 输入输出参数:调用时指定,并且可被改变和返回

 

1)不带参数:

 

mysql>delimiter //

mysql> create procedure p1()    

-> begin     

->  declare v_ename varchar(10);    

->  declare v_sal   int;     

->  select ename,sal into v_ename,v_sal from emp where empno=7788;        ->      select v_ename ;    

->      select v_sal;    

->  end;    

-> //

Query OK, 0 rows affected (0.00 sec)mysql> delimiter ;

mysql> call p1();

 

+---------+

| v_ename |

+---------+

| SCOTT   |

+---------+

1 row in set (0.00 sec)

+-------+

| v_sal |

+-------+

|  3000 |

+-------+

1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

 

2)in参数:

 

mysql> delimiter //

mysql> create procedure p2(in v_empno int)    

-> begin     

->  declare v_ename varchar(10);    

->  declare v_sal   int;     

->  select ename,sal into v_ename,v_sal from emp where empno=v_empno;     ->      select v_ename ;    

->      select v_sal;    

->  end;    

-> //

Query OK, 0 rows affected (0.00 sec)mysql> delimiter ;

 

执行:

mysql> call p2(7788);

 

+---------+

| v_ename |

+---------+

| SCOTT   |

+---------+

1 row in set (0.00 sec)

+-------+

| v_sal |

+-------+

|  3000 |

+-------+

1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

 

3)out参数: 

 

mysql> delimiter //

mysql> create procedure p3(in v_empno int,out v_sal int)    

-> begin     

->  declare v_ename varchar(10);     

->      select ename,sal into v_ename,v_sal from emp where empno=v_empno; ->      select v_ename ;    

->      select v_sal;    

->  end;    

-> //

Query OK, 0 rows affected (0.00 sec)mysql> delimiter ;

 

执行:

 

mysql> call p3(7788,@v_sal);

+---------+

| v_ename |

+---------+

| SCOTT   |

+---------+

1 row in set (0.00 sec)

+-------+

| v_sal |

+-------+

|  3000 |

+-------+

1 row in set (0.00 sec)

 

4)存储过程循环

 

while 循环

 

mysql> delimiter //

mysql> create procedure p_while()

begin

declare v_empno int;

declare v_count int;

       set  v_empno=7788;

       set  v_count=0;

       while v_count < 10 do

             insert into emp1 select * from emp where empno=v_empno;

         set  v_count=v_count + 1;

         end while;

         end;

 //

mysql> delimiter ;

 

repeat 循环:

 

mysql> delimiter //

mysql> create procedure p_repeat()

begin

declare v_empno int;

declare v_count int;

       set  v_empno=7788;

       set  v_count=0;

       repeat

             insert into emp1 select * from emp where empno=v_empno;

         set  v_count=v_count + 1;

         until v_count > 10

         end repeat;

         end;

 //

mysql> delimiter ;

 

loop 循环:

 

mysql> delimiter //

mysql> create procedure p_loop()

begin

declare v_empno int;

declare v_count int;

       set  v_empno=7788;

       set  v_count=0;

       loop_label:loop

             insert into emp1 select * from emp where empno=v_empno;

         set  v_count=v_count + 1;

         if v_count > 10 then

         leave loop_label;

         end if;

         end loop;

         end;

 //

mysql> delimiter ;

 

三、Oracle 存储过程

 

不带参数:

 

SQL> create or replace procedure proc1 is

  3  v_ename emp.ename%type;

  4  v_sal  emp.sal%type ;

  5  

  6  begin

  7      select ename,sal into v_ename,v_sal from emp where empno=&no;

  8      dbms_output.put_line(‘Name is : ‘||v_ename||‘ , ‘||‘Salary is : ‘||v_sal);

  9  exception

 10      when no_data_found then

 11         dbms_output.put_line(‘you number is not crrect ,please input again !‘);

 12  

 13  end;

 14  /

Procedure created

执行:

SQL> exec proc1;

 

PL/SQL procedure successfully completed

 

SQL> set serverout on

SQL> /

 

Name is : SCOTT , Salary is : 3000

 

PL/SQL procedure successfully completed

 

带参数的存储过程:

 

参数定义中,IN、OUT和IN OUT代表参数的三种不同模式: 

 

IN:当调用存储过程时,该模式的形参接收对应实参的值,并且该是只读的,即不能被修改。默认为IN。

 

OUT:该形参被认为只能写,既只能为其赋值。在存储过程中不能读它的值。返回时,将该形参值传给相应的实参。 

 

IN OUT:都允许

 

in 参数:

 

SQL>  create or replace procedure proc2 ( v_empno emp.empno%type) is  

    -- 如果不指明参数的模式,默认为in

  2      v_ename emp.ename%type;

  3      v_sal emp.sal%type;

  4  

  5      begin

  6         select ename,sal into v_ename,v_sal from emp where empno=v_empno;

  7         dbms_output.put_line(‘Name is : ‘||v_ename||‘ , ‘||‘Salary is : ‘||v_sal);

  8      exception

  9          when no_data_found then

 10             dbms_output.put_line(‘you number is not crrect ,please input again !‘);

 11  

 12     end;

 13  /

Procedure created

执行: 

SQL> exec proc2(7369);

 

Name is : SMITH , Salary is : 8000

 

PL/SQL procedure successfully completed

 

SQL> create or replace procedure proc2 ( v_empno in emp.empno%type) is

  2      v_ename emp.ename%type;

  3      v_sal emp.sal%type;

  4  

  5      begin

  6         select ename,sal into v_ename,v_sal from emp where empno=v_empno;

  7         dbms_output.put_line(‘Name is : ‘||v_ename||‘ , ‘||‘Salary is : ‘||v_sal);

  8      exception

  9          when no_data_found then

 10             dbms_output.put_line(‘you number is not crrect ,please input again !‘);

 11  

 12     end;

 13  /

 

out 参数:

 

SQL> create or replace procedure proc2 ( v_empno in emp.empno%type, v_ename out emp.ename%type,v_sal out emp.sal%type)

  2    is

  3  

  4  begin

  5     select ename,sal into v_ename,v_sal from emp where empno=v_empno;

  6     dbms_output.put_line(‘Name is : ‘||v_ename||‘ , ‘||‘Salary is : ‘||v_sal);

  7  exception

  8      when no_data_found then

  9         dbms_output.put_line(‘you number is not crrect ,please input again !‘);

 10  

 11  end;

 12  /

Procedure created

 执行:

SQL> var v_ename varchar2(10);

SQL> var v_sal number;                              //注意:对于number类型不能指明长度

SQL> exec proc2(7788,:v_ename,:v_sal );

 

Name is : SCOTT , Salary is : 3000

 

PL/SQL procedure successfully completed

 

v_ename

---------

SCOTT

v_sal

---------

3000

 

通过匿名块执行:

 

SQL> var v_ename varchar2

SQL> var v_sal number

SQL> declare

  2   v_empno emp.empno%type := 7788;

  3  begin

  4    proc2(v_empno ,:v_ename,:v_sal);

  5  end;

  6  /

  

Name is : SCOTT , Salary is : 3000

  

PL/SQL procedure successfully completed

v_ename

---------

SCOTT

v_sal

---------

3000

 

SQL> declare

  2    v_empno emp.empno%type := 7836;

  3    v_ename emp.ename%type ;

  4   v_sal emp.sal%type;

  5  begin

  6    proc2(v_empno ,v_ename,v_sal);

  7  end;

  8  /

 

in out 参数:

 

SQL>

create or replace procedure proc3( v_empno  in out emp.empno%type)

 is 

 v_ename emp.ename%type;

 v_sal emp.sal%type;

  

 begin

     select ename,sal into v_ename,v_sal from emp where empno=v_empno;

   dbms_output.put_line(‘Name is : ‘||v_ename||‘ , ‘||‘Salary is : ‘||v_sal);

exception

    when no_data_found then

       dbms_output.put_line(‘you number is not crrect ,please input again !‘);     

end;

/

执行:

SQL> var v_empno number;

SQL> exec :v_empno:=7788;

 

PL/SQL procedure successfully completed

v_empno

---------

7788

 

SQL> exec proc3(:v_empno);

 

Employees id is: 7788 , Name is : SCOTT , Salary is : 3000

 

PL/SQL procedure successfully completed

v_empno

---------

7788

 

SQL> declare

  2  v_empno emp.empno%type;

  3  begin

  4   v_empno:=7788;

  5   proc3(v_empno );

  6  end;

  7  /

  

Name is : SCOTT , Salary is : 3000

  

PL/SQL procedure successfully completed

 

SQL> create or replace procedure proc3( v_empno  in out emp.empno%type)

  2  

  3   is

  4  

  5   v_ename emp.ename%type;

  6   v_sal emp.sal%type;

  7  

  8   begin

  9       select empno,ename,sal into v_empno, v_ename,v_sal from emp where empno=v_empno;

 10     dbms_output.put_line(‘Employee number is: ‘||v_empno||‘ , ‘||‘Name is : ‘||v_ename||‘ , ‘||‘Salary is : ‘||v_sal);

 11  exception

 12      when no_data_found then

 13         dbms_output.put_line(‘you number is not crrect ,please input again !‘);

 14  

 15  end;

 16  /

  

Procedure created

 

执行:

 

SQL> declare

  2    v_empno emp.empno%type;

  3  begin

  4    v_empno := 7788;

  5  proc3(v_empno);

  6  end;

  7  /

  

Employee number is: 7788 , Name is : SCOTT , Salary is : 3000

  

PL/SQL procedure successfully completed

 

Oracle 存储过程循环:

 

loop 基本循环:

 

SQL>  declare

  2      v_empno emp.empno%type := 7788;

  3      v_count number :=0;

  4  

  5    begin

  6  

  7      loop

  8         insert into emp1 select * from emp where empno=v_empno;

  9         v_count := v_count + 1;

 10         exit when  v_count >= 10;

 11      end loop;

 12  

 13    end;

 14  /

  

PL/SQL procedure successfully completed

 

for 循环:

 

SQL> declare

  2  

  3     v_empno emp.empno%type;

  4  

  5    begin

  6       v_empno := 7788;

  7  

  8     for  i  in 1..10 loop

  9         insert into emp1 select * from emp where empno=v_empno;

 10     end loop;

 11  

 12   end;

 13  

 14  

 15  

 16  /

  

PL/SQL procedure successfully completed

 

while 循环:

 

SQL> declare

  2      v_empno emp.empno%type;

  3      v_count number := 0;

  4  

  5    begin

  6       v_empno := 7788;

  7  

  8      while v_count < 10  loop

  9         insert into emp1 select * from emp where empno=v_empno;

 10         v_count := v_count + 1;

 11  

 12      end loop;

 13      end;

 14  /

  

PL/SQL procedure successfully completed

mysql与oracle的存储过程有什么区别?