当前位置:Gxlcms > 数据库问题 > oracle pl/sql 函数

oracle pl/sql 函数

时间:2021-07-01 10:21:17 帮助过:11人阅读

--输入雇员的姓名,返回该雇员的年薪
CREATE FUNCTION annual_incomec(uname VARCHAR2)
RETURN NUMBER IS
annual_salazy NUMBER(7,2);
BEGIN
SELECT a.sal*13 INTO annual_salazy FROM emp a WHERE a.ename=uname;
RETURN annual_salazy;
END;
/
技术分享

                   

2)、在sqlplus中调用函数 

SQL> var income NUMBER;
SQL> call annual_incomec(‘SCOTT‘) into:income;
SQL> print income;

    

3)、在java程序中调用oracle函数:select annual_incomec(‘SCOTT‘) income from dual;

技术分享
package junit.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
* 演示java程序调用oracle的函数案例
*
* @author jiqinlin
*
*/
public class ProcedureTest {

public static void main(String[] args) {

try {
// 1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2.得到连接
Connection ct = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "oracle");
// 3.创建PreparedStatement
PreparedStatement ps = ct.prepareStatement("select annual_incomec(‘SCOTT‘) annual from dual");
// 4.执行
ResultSet rs=ps.executeQuery();
if(rs.next()){
Float annual=rs.getFloat("annual");
System.out.println(annual);
}
//5、关闭
rs.close();
ps.close();
ct.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
技术分享

参见:http://www.cnblogs.com/linjiqin/archive/2012/02/24/2367147.html

oracle pl/sql 函数

标签:src   red   out   varchar   包含   body   str   into   drive   

人气教程排行