问题描述
我有一个返回sys_refcursor的函数
这个函数可能会发现有点愚蠢,但我想知道这是否可能。
CREATE OR REPLACE FUNCTION get_employee_details(p_emp_no IN EMP.EMPNO%TYPE)
RETURN SYS_REFCURSOR
AS
o_cursor SYS_REFCURSOR;
BEGIN
OPEN o_cursor FOR
SELECT EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
DEPTNO
FROM emp
WHERE EMPNO = p_emp_no;
RETURN o_cursor;
- 异常部分
END;
/
我可以使用
从双重选择get_employee_details('7369');
是否可以通过指定列名来获得上述函数的结果?
例如如果我想获得ename或工资,我怎么能在sql语句中指定而不使用plsql块?
类似于
select get_employee_details('7369')<特定列>来自双重;
与一个 ref cursor
在所有,否则不是没有创建SQL类型来投入返回,就像这个例子::
创建或替换类型myType作为对象(
a int,
b varchar2(10)
)
/
创建或替换类型myTable作为myType的表;
/
创建或替换函数f1将myTable返回为
l_data myTable:= myTable();
begin
for i in 1..5 loop
l_data.extend;
l_data(i):= myType(i,'Row#'|| i);
结束循环;
返回l_data;
end;
/
select * from TABLE(cast(f1()as myTable));
---------- ----------
1行#1
2行#2
3行#3
4行#4
5行#5
在该线程中发帖:
This may find little silly, but I would like to know whether this is possible.
I have a function which return sys_refcursor
CREATE OR REPLACE FUNCTION get_employee_details(p_emp_no IN EMP.EMPNO%TYPE)
RETURN SYS_REFCURSOR
AS
o_cursor SYS_REFCURSOR;
BEGIN
OPEN o_cursor FOR
SELECT EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
DEPTNO
FROM emp
WHERE EMPNO = p_emp_no;
RETURN o_cursor;
-- exception part
END;
/
and I could get the results using
select get_employee_details('7369') from dual;
Is it possible to get the result from the above function by specifying column name?E.g. If I would want to get ename or salary, how could I specify in the sql statement without using a plsql block?Something like
select get_employee_details('7369') <specific column> from dual;
No, not with a ref cursor
at all, and otherwise not without creating SQL types to cast the return into, like this example: http://dbaspot.com/oracle-server/9308-select-ref-cursor.html:
create or replace type myType as object (
a int,
b varchar2(10)
)
/
create or replace type myTable as table of myType;
/
create or replace function f1 return myTable as
l_data myTable := myTable();
begin
for i in 1 .. 5 loop
l_data.extend;
l_data(i) := myType(i, 'Row #'||i );
end loop;
return l_data;
end;
/
select * from TABLE ( cast( f1() as myTable ) );
---------- ----------
1 Row #1
2 Row #2
3 Row #3
4 Row #4
5 Row #5
From the last post on that thread:
这篇关于函数返回具有特定列的sql的sys_refcursor调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!