15.数据库对象----存储过程(procedure)

来源:互联网 发布:天龙八部游戏 知乎 编辑:程序博客网 时间:2024/05/19 20:00

    语法:
        create or replace procedure procedure_name
           【(param_name [mode] param_type,.... )】
       is/as
          【param_name param_type [:=value]】
       begin

       end [procedure_name]
     注意:   mode
                in
                     例子:创建 mode 为 in
                               create or replace procedure curr_time
                               is
                               curr char(20);
                               begin
                                   curr:=to_char(sysdate,'yyyy-mm-dd hh24:mi:ss');
                                   dbms_output.put_line(curr);
                               end curr_time;
                out
                     例子:创建 mode 为 out
                               create or replace procedure curr_time(curr_time date,res_time out char)
                               is
                               begin
                                   res_time:=to_char(curr_time,'yyyy-mm-dd hh24:mi:ss');
                              end curr_time;
                in out
    使用:只能在pl/sql中使用
                没有返回数据,即mode都为in
                -----------使用 begin curr_time; end;
                有返回数据,即有一个out/ in out 或者多个
                -----------使用 declare res_time
                                         char(20);
                                        begin
                                             curr_time(sysdate,res_time);
                                             dbms_output.put_line(res_time);
                                        end;
    和函数的区别:
      1.  语法不同
                1.关键字不同:  函数是function,存储过程是procedure
                2.存储过程没有返回值描述部分,而函数必须有
      2.  返回值方式不同
                存储过程不能够使用return返回数据,只能使用out 或 in out 类型的参数返回数据
                函数必须使用return 返回数据,如果要返回多个结果则可以使用out/in out 类型数据返回
       3. 用法不同
                存储过程不能用于DML/DQL语句中,只能用于pl/sql中
                函数在DML/DQL/pl/sql中都可以使用
                参数模型为out或 in out 的函数除外, 只能在pl/sql中使用
                     在语句中无法使用mode 为out的参数,用来赋值或者计算
        4. 函数不能单独使用,必须作为表达式的一部分,存储过程可以单独使用  
                  函数和存储过程不分家,只是使用的地方不同: 
原创粉丝点击