本文通过一个例子演示,基本涵盖了Oracle PL/SQL中的大部分知识点,但只是基本使用,敬请参考
创建表
create table test_table(
t_index number(4) primary key not null,
t_name varchar2(5),
t_value number(4)
);
创建序列
create sequence test_sequence start with 1 increment by 1;
创建触发器
create or replace trigger test_trigger
before insert on test_table for each row
declare
v_t_index test_table.t_index%type;
begin
select test_sequence.nextval into v_t_index from dual;
:NEW.t_index := v_t_index;
end;
创建包含输出游标(动态SQL)的包
create or replace package cursor_package is
type out_cursor is ref cursor;
end cursor_package;
创建Oracle对象
create or replace type test_object as object(
v_t_index number,
v_t_name varchar2(5),
v_t_value number
)
创建嵌套集合类型
create or replace type test_object_table as table of test_object
创建存储过程
--注释
create or replace procedure test_procedure(
p_in1 in number, --传入参数 内置类型 (参数名称 参数类型)
p_in2 in test_table.t_index%type, --传入参数 声明为表列对应的类型
p_out out cursor_package.out_cursor --传出参数 声明为游标类型
)
authid current_user --存储过程中使用role权限
as
type t_name_table is table of test_table.t_name%type index by binary_integer; --声明表结构
type t_value_table is table of test_table.t_value%type index by binary_integer;
t_name_tables t_name_table; --声明联合数组
t_value_tables t_value_table;
v_sql varchar2(100);
v_t_tables test_object_table := test_object_table(); --声明嵌套表
v_t_index test_table.t_index%type;
v_t_name test_table.t_name%type;
v_t_value test_table.t_value%type;
cursor c_test_table is --声明游标
select t_index, t_name, t_value from test_table where t_index=p_in1 or t_index=p_in2 order by t_index;
v_index number;
begin
v_index := 1;
for i in 1..10 loop
t_name_tables(i) := 'row' || i;
t_value_tables(i) := i;
end loop;
for i in 1..10 loop
insert into test_table(t_name, t_value) values(t_name_tables(i), t_value_tables(i));
end loop;
v_sql := 'create table test_table1 as select * from test_table';
execute immediate v_sql; --动态SQL(创建新表)
open c_test_table; --打开游标
loop
exit when c_test_table%notfound;
fetch c_test_table into v_t_index, v_t_name, v_t_value;
v_t_tables.extend;
v_t_tables(v_index) := test_object(v_t_index, v_t_name, v_t_value); --给嵌套表赋值
v_index := v_index + 1;
end loop;
close c_test_table;
open p_out for select * from Table(cast(v_t_tables as test_object_table)); --打开游标(传出参数)
end test_procedure;
以上代码在PLSQL Developer上运行即可(全部通过测试)
调用存储过程的Java代码(jsp实现)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="oracle.jdbc.OracleTypes"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<% ///////////////////////////////////////////////////////////////////////
Connection con = null;
CallableStatement csmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:LANG", "scott",
"tiger");
csmt = con.prepareCall("{call test_procedure(?, ?, ?)}");
csmt.setInt(1,1);
csmt.setInt(2,2);
csmt.registerOutParameter(3, OracleTypes.CURSOR);
csmt.execute();
rs = (ResultSet)csmt.getObject(3);
while(rs.next()) {
out.println(rs.getString(1));
out.println(rs.getString(2));
out.println(rs.getString(3));
}
} catch(Exception e) {
System.out.println(e.getMessage());
} finally {
if(null != rs) {
rs.close();
}
if(null != csmt) {
csmt.close();
}
if(null != con) {
con.close();
}
}
//////////////////////////////////////////////////////////////////////////
%>
</body>
</html>
代码中的
////////////////////////////
////////////////////////////
部分是调用存储过程的核心代码
本实例只要按照代码排版的顺序依次执行,即可通过测试,希望对阁下的Oracle PL/SQL学习有所帮助
分享到:
相关推荐
《Oracle PL/SQL程序设计(第5版)(套装上下册)》基于Oracle数据库11g,从PL/SQL编程、PL/SQL程序结构、PL/SQL程序数据、PL/SQL中的SQL、PL/SQL应用构建、高级PL/SQL主题这6个方面详细系统地讨论了PL/SQL以及如何...
PL/SQL是Oracle对标准数据库语言的扩展,Oracle公司已经将PL/SQL整合到Oracle 服务器和其他工具中了,近几年中更多的开发人员和DBA开始使用PL/SQL,本教程将以循速渐进的方式讲述PL/SQL基础语法,结构和组件、以及...
Oracle PL/SQL实例精解 student模式 数据库建立代码 包括数据库的建立、索引等。样本数据的插入。
Oracle PL/SQL实例编程 Oracle PL/SQL实例编程 Oracle PL/SQL实例编程
oracle PL/SQL pdf学习资料oracle PL/SQL pdf学习资料oracle PL/SQL pdf学习资料oracle PL/SQL pdf学习资料oracle PL/SQL pdf学习资料
oracle pl/sql实例精讲student数据库模式
Oracle PL/SQL学习官方教材,英文版,覆盖了PL/SQL的所有知识,适合入门者或作为参考资料.
Topics covered: How to use Oracle PL/SQL in all its manifestations through Oracle9i. Fundamentals of program structure (loops, cases, exceptions, etc.) and execution get attention, as do data types, ...
NULL 博文链接:https://elf8848.iteye.com/blog/740881
Considered the best Oracle PL/SQL programming guide by the Oracle community, this definitive guide is precisely what you need to make the most of Oracle’s powerful procedural language. The sixth ...
Oracle PL/SQL专家指南-高级PL/SQL解决方案的设计与开发
ORACLE PL/SQL编程之八ORACLE PL/SQL编程之八ORACLE PL/SQL编程之八ORACLE PL/SQL编程之八
oracle pl/sql从入门到精通 配套源代码
《Oracle PL/SQL程序设计(第5版)》基于Oracle数据库11g,从PL/SQL编程、PL/SQL程序结构、PL/SQL程序数据、PL/SQL中的SQL、PL/SQL应用构建、高级PL/SQL主题这6个方面详细系统地讨论了PL/SQL以及如何有效地使用它。...
《oracle pl/sql程序设计(第5版)》基于oracle数据库11g,从pl/sql编程、pl/sql程序结构、pl/sql程序数据、pl/sql中的sql、pl/sql应用构建、高级pl/sql主题6个方面详细系统地讨论了pl/sql以及如何有效地使用它。...
《Oracle PL/SQL程序设计(第5版)》基于Oracle数据库11g,从PL/SQL编程、PL/SQL程序结构、PL/SQL程序数据、PL/SQL中的SQL、PL/SQL应用构建、高级PL/SQL主题这6个方面详细系统地讨论了PL/SQL以及如何有效地使用它。...
这是我自己根据书本《ORACLE PL/SQL从入门到精通》何明,写的笔记,有目录。
oracle10g pl/sql完备教程,供初学者学习与开发者参考
Oracle PL/SQL程序设计,经典书籍,既可以当教材学习,也可以当手册查询。
oracle pl/sql 编程大全,希望能对想学oracle或者oracle新手以及需要用到这方面知识的朋友有点帮助。