参照弱水三千的代码:测试过程:
1、建立测试表
CREATE TABLE student
(
id NUMBER,
name VARCHAR2(30),
sex VARCHAR2(10),
address VARCHAR2(100),
postcode VARCHAR2(10),
birthday DATE,
photo LONG RAW
)
/
2、建立带ref cursor定义的包和包体及函数:
CREATE OR REPLACE
package pkg_test as
/* 定义ref cursor类型
不加return类型,为弱类型,允许动态sql查询,
否则为强类型,无法使用动态sql查询;
*/
type myrctype is ref cursor;
--函数申明
function get(intID number) return myrctype;
end pkg_test;
/
CREATE OR REPLACE
package body pkg_test as
--函数体
function get(intID number) return myrctype is
rc myrctype; --定义ref cursor变量
sqlstr varchar2(500);
begin
if intID=0 then
--静态测试,直接用select语句直接返回结果
open rc for select id,name,sex,address,postcode,birthday from student;
else
--动态sql赋值,用:w_id来申明该变量从外部获得
sqlstr := 'select id,name,sex,address,postcode,birthday from student where id=:w_id';
--动态测试,用sqlstr字符串返回结果,用using关键词传递参数
open rc for sqlstr using intid;
end if;
return rc;
end get;
end pkg_test;
/
3、用pl/sql块进行测试:
declare
w_rc pkg_test.myrctype; --定义ref cursor型变量
--定义临时变量,用于显示结果
w_id student.id%type;
w_name student.name%type;
w_sex student.sex%type;
w_address student.address%type;
w_postcode student.postcode%type;
w_birthday student.birthday%type;
begin
--调用函数,获得记录集
w_rc := pkg_test.get(1);
--fetch结果并显示
fetch w_rc into w_id,w_name,w_sex,w_address,w_postcode,w_birthday;
dbms_output.put_line(w_name);
end;
1、建立测试表
CREATE TABLE student
(
id NUMBER,
name VARCHAR2(30),
sex VARCHAR2(10),
address VARCHAR2(100),
postcode VARCHAR2(10),
birthday DATE,
photo LONG RAW
)
/
2、建立带ref cursor定义的包和包体及函数:
CREATE OR REPLACE
package pkg_test as
/* 定义ref cursor类型
不加return类型,为弱类型,允许动态sql查询,
否则为强类型,无法使用动态sql查询;
*/
type myrctype is ref cursor;
--函数申明
function get(intID number) return myrctype;
end pkg_test;
/
CREATE OR REPLACE
package body pkg_test as
--函数体
function get(intID number) return myrctype is
rc myrctype; --定义ref cursor变量
sqlstr varchar2(500);
begin
if intID=0 then
--静态测试,直接用select语句直接返回结果
open rc for select id,name,sex,address,postcode,birthday from student;
else
--动态sql赋值,用:w_id来申明该变量从外部获得
sqlstr := 'select id,name,sex,address,postcode,birthday from student where id=:w_id';
--动态测试,用sqlstr字符串返回结果,用using关键词传递参数
open rc for sqlstr using intid;
end if;
return rc;
end get;
end pkg_test;
/
3、用pl/sql块进行测试:
declare
w_rc pkg_test.myrctype; --定义ref cursor型变量
--定义临时变量,用于显示结果
w_id student.id%type;
w_name student.name%type;
w_sex student.sex%type;
w_address student.address%type;
w_postcode student.postcode%type;
w_birthday student.birthday%type;
begin
--调用函数,获得记录集
w_rc := pkg_test.get(1);
--fetch结果并显示
fetch w_rc into w_id,w_name,w_sex,w_address,w_postcode,w_birthday;
dbms_output.put_line(w_name);
end;
解决方案 »
- 请教一个统计的查询语句
- 如何设置在sqlplus中不以科学计数法显示
- 关于在存储过程中使用dbms_xslprocessor的问题.
- 我想把B表中的fristname,lastname字段插到A表中的fristname,lastname字段中,咋办?
- 求助:为什么我的这个触发器中的:old,:new不起作用?
- 同样的程序在不同的操作系统下连接oracle速度的不同反应!
- 调用存储过程出错现象:在存储过程中使用not in ()方式引用传入字符型值(字符型值之间使用“,”分隔)时总是出错。对于传值应注意什么事
- 请问如何将资料库中的所有用户存储过程的文本导出到一个文件中
- 请教:在一张表中怎么取出A列大于B列的所有行
- 求201612某部门的上月结存(存休+特休)-本月请假+本月加班=本月结存
- 哪里可以download OCP的材料?
- 诸位谁有discoverer告诉好吗?或告诉我哪有下载的。我100分赠送,多谢了。
谢谢燕赤霞,我现在用的就是VIEW,不过我想试试返回结果集的Procedure.To zys2000(脚底揩油):
我改动后的过程是照Oracle的ONline document来写的,与弱水三千是同一出处,问题是从Cursor返回的代码而不是名称,我又不想在前端处理,我想在过程中处理完后直接返回名称。
Oracle中国 0001 王五 开发部 Developer
而从employee表中查出的是:comp_id,emp_id,emp_name,dept_id,job_title
对应id的相应名称,又要从另外的表中得到。由于实际工作中,要通过10几个表的联接才能得到最后结果,所以我不想用VIEW。
CURSOR c_employee IS
SELECT c.comp_name,emp_id,emp_name,d.dept_name,j.job_name
FROM employee e,company c,department d,job j
WHERE e.comp_id = p_company AND e.dept_id = p_dept
and e.comp_id=c.comp_id --与company联结
and e.dept_id=d.dept_id --与department联结
and e.job_id = j.job_id; --与job联结