1、存储过程和函数的概念:

ORACLE 提供可以把PL/SQL 程序存储在数据库中,并可以在任何地方来运行它。这样就叫存储过程或函数。过程和函数统称为PL/SQL子程序,他们是被命名的PL/SQL块,均存储在数据库中,并通过输入、输出参数或输入/输出参数与其调用者交换信息。过程和函数的唯一区别是函数总向调用者返回数据,而过程则不返回数据

常见的单词:

pragma 编译指示

instantiable  实例化

overriding 覆盖重写

static member 静态成员

delimited 划定…的界限

identifier 标识符

reverse 反向

2、存储函数的格式

|存储函数格式|

create or replace function func_name(dept_id number,salary number)

return varchar2

is

  -- 函数使用过程中,需要声明的变量,记录类型,cursor

      

begin 

  --函数的执行体,如果有返回值需要return

    return 'helloworld'|| v_logo;

       --commit;如果此处是进行insert、delete、update操作,可以通过提交进行更改,无需再调用存储函数后再进行提交

exception 

   --处理函数执行过程中的异常

end;

详解:

1)因为函数需要返回一个值, 所以RETURN 包含返回结果的数据类型.

2)函数名后面是一个可选的参数列表, 其中包含IN, OUTIN OUT 标记. 参数之间用逗号隔开

IN 参数标记表示传递给函数的值在该函数执行中不改变;

OUT 标记表示一个值在函数中进行计算并通过该参数传递给调用语句;

IN OUT 标记表示传递给函数的值可以变化并传递给调用语句. 若省略标记, 则参数隐含为IN

3

A、存储函数:有返回值,创建完成后,如何调用:

方法一

select function() from dual;

方法二:

set serveroutput on;
var aaa varchar2(10);
call hello_world() into :aaa;

方法三:

begin
    dbms_output.put_line(hello_world());
end;

B、存储过程:由于没有返回值,创建完成后,不能使用select语句,只能使用pl/sql块执行

|实例一:创建一个无参函数| 

create or replace function hello_world
return varchar2
is
begin 
    return 'hello world';
end;

如何调用:

方法一

select hello_world() from dual;

方法二

set serveroutput on;
var aaa varchar2(10);
call hello_world() into :aaa;

方法三:

begin
    dbms_output.put_line(hello_world());
end;

方法四:

declare
      v_bianling varchar2(40);
begin
      v_bianling := hello_world;
      dbms_output.put_line(v_bianling);
end;

|实例二:创建一个有参函数(传进去的参数v_classid的值是不变的)| 

求一个班学生的总薪水

create or replace function get_sal(v_classid number)
return number
is 
       v_sumsal number(10):=0;
       cursor sal_cursor is select sal from  student where classid =v_classid;  
begin
       for c in sal_cursor loop
      v_sumsal:=v_sumsal+c.sal;
      end loop;
      return v_sumsal;
 end;

 

|实例三:创建一个有参函数(传进去的参数v_classid的值是不变的)| 

OUT型参数  对于实例二中的传进去的参数一般是不变的

In是输入类型,而out是输入输出类型的

如果一个形参用out修饰 那么它就会在函数中被赋值,并且可以当成一个数传出去(结合例子理解)

create or replace function get_sal1(v_classid number,total_sal out number)
return number
is 
       v_sumsal number(10):=0;
       cursor sal_cursor is select sal from  student where classid =v_classid;  
begin
       total_sal:=0;
       for  c in sal_cursor loop
       v_sumsal:=v_sumsal+c.sal;
       total_sal:=total_sal +1;
       end loop;
       return v_sumsal;
 end;

 

如何调用:

declare
       v_total_sal number(5);
begin
       dbms_output.put_line(get_sal1(1, v_total_sal));
       dbms_output.put_line(v_total_sal);
end;

 

无返回值

create or replace function get_sal2(v_classid number,total_sal out number)
return number
is
       cursor sal_cursor is select sal from  student where classid =v_classid;  
begin
       total_sal:=0;
       for  c in sal_cursor loop
      total_sal:= total_sal+c.sal;
      end loop;
return total_sal;
 end;

如何调用:

declare 
 v_classid   number(5):=1;
       v_total_sal  number(10):=0;
begin 
      dbms_output.put_line(get_sal2(v_classid,v_total_sal));
      dbms_output.put_line(v_total_sal);
end;

3.plsqldep存储过程如何打断点进行调试

1)edit pkg_regulatory_tax.;  
在相应的代码处添加断点、点击执行按钮进行编译
2)test pkg_regulatory_tax.p_load_auto;输入传参信息,点击上面的执行按钮进行调试

postgresql 存储过程 终止循环 plsql存储过程打断点_有参函数

 

细水长流,打磨濡染,渐趋极致,才是一个人最好的状态。