Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > Creating a Stored Procedure or Function in oralce
【标  题】:Creating a Stored Procedure or Function in oralce
【关键字】:Creating,Stored,Procedure,or,Function,in,oralce
【来  源】:http://www.cublog.cn/u/14415/showart.php?id=99299

Creating a Stored Procedure or Function in oralce

Your Ad Here
A stored procedure or function can be created with no parameters, IN parameters, OUT parameters, or IN/OUT parameters. There can be many parameters per stored procedure or function.

An IN parameter is a parameter whose value is passed into a stored procedure/function module. The value of an IN parameter is a constant; it can't be changed or reassigned within the module.

An OUT parameter is a parameter whose value is passed out of the stored procedure/function module, back to the calling PL/SQL block. An OUT parameter must be a variable, not a constant. It can be found only on the left-hand side of an assignment in the module. You cannot assign a default value to an OUT parameter outside of the module's body. In other words, an OUT parameter behaves like an uninitialized variable.

An IN/OUT parameter is a parameter that functions as an IN or an OUT parameter or both. The value of the IN/OUT parameter is passed into the stored procedure/function and a new value can be assigned to the parameter and passed out of the module. An IN/OUT parameter must be a variable, not a constant. However, it can be found on both sides of an assignment. In other words, an IN/OUT parameter behaves like an initialized variable.

This example creates stored procedures and functions demonstrating each type of parameter.

See also e281 Calling a Stored Procedure in a Database and e282 Calling a Function in a Database.

    try {
        // To create a connection to an Oracle database,
        // see e235 Connecting to an Oracle Database
        Statement stmt = connection.createStatement();
    
        // Create procedure myproc with no parameters
        String procedure =
            "CREATE OR REPLACE PROCEDURE myproc IS "
            + "BEGIN "
            + "INSERT INTO oracle_table VALUES('string 1'); "
            + "END;";
        stmt.executeUpdate(procedure);
    
        // Create procedure myprocin with an IN parameter named x.
        // IN is the default mode for parameter, so both `x VARCHAR' and `x IN VARCHAR' are valid
        procedure =
            "CREATE OR REPLACE PROCEDURE myprocin(x VARCHAR) IS "
            + "BEGIN "
            + "INSERT INTO oracle_table VALUES(x); "
            + "END;";
        stmt.executeUpdate(procedure);
    
        // Create procedure myprocout with an OUT parameter named x
        procedure =
            "CREATE OR REPLACE PROCEDURE myprocout(x OUT VARCHAR) IS "
            + "BEGIN "
            + "INSERT INTO oracle_table VALUES('string 2'); "
            + "x := 'outvalue'; " // Assign a value to x
            + "END;";
        stmt.executeUpdate(procedure);
    
        // Create procedure myprocinout with an IN/OUT parameter named x;
        // x functions as an IN parameter and also as an OUT parameter
        procedure =
            "CREATE OR REPLACE PROCEDURE myprocinout(x IN OUT VARCHAR) IS "
            + "BEGIN "
            + "INSERT INTO oracle_table VALUES(x); " // Use x as IN parameter
            + "x := 'outvalue'; "                    // Use x as OUT parameter
            + "END;";
        stmt.executeUpdate(procedure);
    
        // Create a function named myfunc which returns a VARCHAR value;
        // the function has no parameter
        String function =
            "CREATE OR REPLACE FUNCTION myfunc RETURN VARCHAR IS "
            + "BEGIN "
            + "RETURN 'a returned string'; "
            + "END;";
        stmt.executeUpdate(function);
    
        // Create a function named myfuncin which returns a VARCHAR value;
        // the function has an IN parameter named x
        function =
            "CREATE OR REPLACE FUNCTION myfuncin(x VARCHAR) RETURN VARCHAR IS "
            + "BEGIN "
            + "RETURN 'a return string'||x; "
            + "END;";
        stmt.executeUpdate(function);
    
        // Create a function named myfuncout which returns a VARCHAR value;
        // the function has an OUT parameter named x whose value is
        // returned to the calling PL/SQL block when the execution of the function ends
        function =
            "CREATE OR REPLACE FUNCTION myfuncout(x OUT VARCHAR) RETURN VARCHAR IS "
            + "BEGIN "
            + "x:= 'outvalue'; "
            + "RETURN 'a returned string'; "
            + "END;";
        stmt.executeUpdate(function);
    
        // Create a function named myfuncinout that returns a VARCHAR value;
        // the function has an IN/OUT parameter named x.  As an IN parameter, the value of x is
        // defined in the calling PL/SQL block before it is passed in eyfuncinout
        // function.  As an OUT parameter, the new value of x, `x value||outvalue', is also
        // returned to the calling PL/SQL block when the execution of the function ends.
        function =
            "CREATE OR REPLACE FUNCTION myfuncinout(x IN OUT VARCHAR) RETURN VARCHAR IS "
            + "BEGIN "
            + "x:= x||'outvalue'; "
            + "RETURN 'a returned string'; "
            + "END;";
        stmt.executeUpdate(function);
    } catch (SQLException e) {
    }
点击关闭窗口:【上一篇】
Calling a Function in a Database:【下一篇】
【相关文章】
  • Aix下使用rman备份Oracle RAC数据库
  • RAC下修改oracle归档模式
  • Oracle数据库的备份方法-冷备份
  • 使用export作为备份策略
  • Oracle数据库RMAN备份与恢复技术
  • Oracle 中使用层次查询方便处理财务报表
  • 取得form里checkbox的value值
  • 我常用的LINUX命令
  • calcru: negative runtime of -106344 usec for pid .
  • 用portsnap升级FreeBSD 6.0的port tree
  • 【随机文章】
  • 《东方时代环球时事解读.时事节简版》 星期五 05年11月11日
  • 在IE中实现窗口间操作下拉选框的选项
  • javax.transaction.Synchronization翻译
  • varyonvg提示0516-013错误信息
  • Perle Jetstream4000 (远程访问服务器)
  • 安装,配置rp-pppoe拨号软件,使adsl成功上网
  • AIX内核参数调整之vmtune -r -R(4)?
  • java中properties文件的使用
  • vim编辑器使用[学习札记]
  • 查看最近一次访问文件的时间
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.