`

plsql package - example

    博客分类:
  • db
 
阅读更多

plsql package - example

 

an example to use plsql package/function/procedure of oracle,

 

include:

  • package/functipn/procedure define
  • sys_refcursor use
  • call from plsql
  • call from jdbc

------

table

 

create table customer(
	customer_id NUMBER(10),
	customer_name VARCHAR2(100),
	address varchar2(200),
	primary key(customer_id)
);

create table orders(
	order_id NUMBER(10),
	order_date DATE,
	order_name VARCHAR2(200),
	customer_id NUMBER(10),
	primary key(order_id)
);
 

------

data

 

-- customer
declare
  n CONSTANT NUMBER(4) := 5; -- row count
  i NUMBER(4);
begin
  for i in 1..n loop
	insert into customer values (i, concat('eric',i), 'shenzhen');
  end loop;
end;

-- orders
declare
  n CONSTANT NUMBER(4) := 5; -- customer row count
  i NUMBER(4);  -- customer id
  j NUMBER(2);
  order_id NUMBER(6) := 1;
begin
  for i in 1..n loop
    for j in 1..i loop
	insert into orders values (order_id, to_date('2010-3-21','YYYY-MM-DD'), concat('order_',order_id), i);
      order_id:=order_id+1;
    end loop;
  end loop;
end;
 

 

------

package

 

create or replace
PACKAGE pkg_orders_by_customer
AS
	procedure proc_orders_by_customer(p_customer_id in orders.customer_id%type, order_ref_cursor out SYS_REFCURSOR);
	function fun_order_count(p_customer_id in NUMBER) return NUMBER;
END;
  

------

package body

 

create or replace PACKAGE body pkg_orders_by_customer AS
	procedure proc_orders_by_customer(p_customer_id in orders.customer_id%type, order_ref_cursor out SYS_REFCURSOR)
	IS
	BEGIN
		open order_ref_cursor for
		select c.customer_id, o.order_id, o.order_name, o.order_date
		from customer c join orders o on c.customer_id=o.customer_id
		where o.customer_id = p_customer_id;      
	END proc_orders_by_customer;

	function fun_order_count(p_customer_id in NUMBER) return NUMBER
	IS
	order_count NUMBER;
	BEGIN
		select count(*) into order_count from orders o where o.customer_id=p_customer_id;
		return order_count;
	END fun_order_count;
END;
  

------

call function - plsql

 

set serveroutput on;

declare
  cid NUMBER(5);
  ocount NUMBER(5);
  n NUMBER(4):=5; -- customer count
begin
  for cid in 1..n loop
    ocount := pkg_orders_by_customer.fun_order_count(p_customer_id => cid);
    dbms_output.put_line('customer_id: ' || cid || ', order_count: ' || ocount);
  end loop;
end;
  

------

call procedure - plsql

 

set serveroutput on;

declare
  n NUMBER(4):=5; -- customer count
  cid NUMBER(5);
  v_cursor SYS_REFCURSOR;
  v_customer_id customer.customer_id%type;
  v_order_id orders.order_id%type;
  v_order_name orders.order_name%type;
  v_order_date orders.order_date%type;  
begin
  for cid in 1..n loop
    pkg_orders_by_customer.proc_orders_by_customer(p_customer_id => cid, order_ref_cursor => v_cursor);
    loop
      fetch v_cursor 
      into v_customer_id, v_order_id, v_order_name, v_order_date;
      EXIT WHEN v_cursor%NOTFOUND;
      dbms_output.put_line(v_customer_id || ', ' || v_order_id || ', ' || v_order_name || ', ' || v_order_date);
    end loop;
    close v_cursor;
    dbms_output.new_line();
  end loop;
end;
 

------

call function - jdbc

 

public static void functionTest() {
	try {
		Class.forName("oracle.jdbc.driver.OracleDriver");

		String url, user, password;
		url = "jdbc:oracle:thin:@localhost:1521:xe";
		user = "cumtspace";
		password = "abcdefg";

		// create connection
		Connection conn = DriverManager.getConnection(url, user, password);

		// create CallableStatement
		CallableStatement cstmt = conn.prepareCall("{?=call pkg_orders_by_customer.fun_order_count(?)}");
		cstmt.registerOutParameter(1, Types.INTEGER);
		int customer_id = 4;
		cstmt.setInt(2, customer_id);
		// execute
		cstmt.execute();
		// get out param
		Integer count = cstmt.getInt(1);
		System.out.println(customer_id + ", " + count);

		cstmt.close();
		conn.close();
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}
}
 

------

call procedure - jdbc

 

public static void procedureTest() {
	try {
		Class.forName("oracle.jdbc.driver.OracleDriver");

		String url, user, password;
		url = "jdbc:oracle:thin:@localhost:1521:xe";
		user = "cumtspace";
		password = "abcdefg";

		// create connection
		Connection conn = DriverManager.getConnection(url, user, password);

		// create CallableStatement
		CallableStatement cstmt = conn.prepareCall("{call pkg_orders_by_customer.proc_orders_by_customer(?,?)}");

		// set in/out params
		int cid = 4;
		cstmt.setInt(1, cid);
		cstmt.registerOutParameter(2, OracleTypes.CURSOR);

		// execute
		cstmt.execute();

		// get ResultSet from out param
		ResultSet rs = (ResultSet) cstmt.getObject(2);

		// handle resultset
		int customer_id, order_id;
		String order_name;
		Date order_date;
		while (rs.next()) {
			customer_id = rs.getInt(1);
			order_id = rs.getInt(2);
			order_name = rs.getString(3);
			order_date = rs.getDate(4);

			System.out.format("%5d\t%5d\t%15s\t%s\n", customer_id, order_id, order_name, order_date);
		}

		// close
		rs.close();
		cstmt.close();
		conn.close();
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}
}
 

------


 

分享到:
评论

相关推荐

    PLSQL笔记-从hello word到触发器,包,游标高级应用

    PLSQL,全称为Procedural Language/SQL,是Oracle数据库提供的结构化查询语言扩展,它结合了SQL(Structured Query Language)的查询功能和过程性编程语言的控制结构,用于开发在Oracle环境中运行的应用程序。...

    Oracle Plsql By Example 4Th-代码示例-11g.rar

    10. **包(Package)**:包是将相关的常量、类型、过程和函数封装在一起的高级结构,有助于代码组织和复用,同时提供私有化和隐藏的功能。 11. **索引优化**:了解如何使用索引来加速查询性能,以及何时使用索引和...

    PLSQL By Example 4th Edition

    《PLSQL By Example 4th Edition》是一本详细介绍PL/SQL编程基础的书籍,特别针对Oracle 11g的新特性进行了深入的讲解。PL/SQL是Oracle数据库中的过程化语言,它结合了SQL的查询能力与传统的编程语言元素,使得...

    Oracle+10G+-+Plsql+User's+Guide

    What's New in PL/SQL? This section describes new features of PL/SQL release 10g, and... For example, a package body can be compiled natively while the package spec is compiled interpreted, or vice versa.

    oracle_PLSQL_语法详细手册

    - **PACKAGE**:用于组织和管理 PL/SQL 对象(如过程、函数)。 - **触发器**:当特定事件发生时自动执行的一段代码。 - **应用实例**:提供了多个实际场景下的应用示例,帮助理解如何在实际项目中运用 PL/SQL。 ...

    oralce存储过程包跨用户访问表

    CREATE OR REPLACE PACKAGE pkg_example AS PROCEDURE proc_query_data; END pkg_example; / ``` 然后,创建包体实现具体的逻辑。 ```plsql CREATE OR REPLACE PACKAGE BODY pkg_example AS PROCEDURE proc_...

    11g_plsql_user_guide_and_reference.pdf

    For example, they can specify whether a trigger should be executed before or after a DML operation, or whether it should be executed for each row or for the entire statement. This level of control ...

    Oracle PL/SQL实例编程(PL/SQL经典书籍)

    通过“Oracle PLSQL by Example, Third Edition”这样的书籍,你可以期待学习到上述知识的详细解释和实际应用示例。书中可能包含各种类型的PL/SQL程序设计技巧,从简单的存储过程和函数到复杂的事务处理和并发控制...

    Install & Config APEX

    # The following configuration is an example of how to configure the # mod_plsql module for Oracle Application Express. # # Note: Please ensure that the database and the web server are properly # ...

Global site tag (gtag.js) - Google Analytics