做到这一点的最好方法是使用游标变量,通常称为参考游标。这基本上是一个指向结果集的指针。一个REF CURSOR的好处是,我们可以改变select语句,就像这样:
create or replace package student_utils is
-- a hard-types ref cursor
type stud_cur is ref cursor return students%rowtype;
function get_students
(p_sid in students.sid%type := null)
return stud_cur;
end;
请注意,我已经决定了你是如何打算使用代码夫妇推定。使用包可以让我们定义一个硬类型的游标,这意味着它只能用于匹配STUDENTS表的投影的查询。 (如果你没有一个学生实际表,你可以使用的意见或定义一个PL/SQL记录来代替。)
create or replace package body student_utils is
function get_students
(p_sid in students.sid%type := null)
return stud_cur
is
return_value stud_cur;
begin
if p_sid is null
then
open return_value for
select * from m_viewallStudents;
else
open return_value for
select * from m_viewStudent
where sid = p_sid;
end if;
return return_value;
end;
end;
这些查询都是硬编码的,但我们可以用动态的同时打开引用游标SQL,这是一个强大的技术。
阅读文档至find out more.
来源
2013-02-03 11:58:57
APC
为什么这会被关闭为“off topic”?这个问题似乎决定于编程和软件开发。如果没有,有很多SQL问题需要关闭... –