Ref cursor in select statement
I am not sure this is the right forum, but I got no reply from the OTN Sample.
How do I call a function from a select
statement ?
The function returns a ref cursor.
Here is the function definition:
FUNCTION get_priority_code_list
(p_language_id IN VARCHAR2)
RETURN RefCur
IS
rcur RefCur;
BEGIN
OPEN rcur FOR SELECT
priority_code "priority_code_menu",
priority_code_desc "priority_code_menu_desc"
FROM
prio_desc
WHERE
language_id = p_language_id;
RETURN rcur;
END;
I would like to wrap a SELECT statement
around the function and have the same result
as running the SELECT statement directly.
Of course it does not seem logical here to
use a function, but in fact I want the
function to do more than just a select....
I tried the syntax
SELECT package.function(param) FROM dual;
but that does not work...
or
SELECT * FROM package.function(param);
but that does not work either....
any idea ?
Tks.
null
I have a stored function that returns a ref cursor and I want to use it in select
I tried the following code
for example if the ref cursor returned rows of varchar2
I will define an object
CREATE TYPE myType as OBJECT( doc_type VARCHAR2(256));
created a type as a table of myType
create or replace type myTable as table of myType;
If I write a select statement like below
select * from TABLE(CAST(function_name() AS myTable))
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got CURSER
I get this error
Can anyone give any clue why this does throw an error or if there is a second way to do this,
Similar Messages
-
Hi,
Please let me know what is the difference between using OPEN CURSOR and SELECT statement WITH PACAKGE additon.
PraneethHi,
http://wiki.sdn.sap.com/wiki/display/ABAP/SELECTStatementsandCURSORstatement-Performance+Analysis.
and also check
http://help.sap.com/saphelp_46c/helpdata/en/fc/eb3b23358411d1829f0000e829fbfe/content.htm
cheers,
Bhavana -
Using Cursor in Select statements? How to do this?
I am getting an error whilt passing a cursor to a select clause:
SELECT dbms_xmlquery.getXML('select deptno, dname, '||
'cursor(select empno, ename, sal from emp e where e.deptno = d.deptno) employees '||
'from dept d where d.deptno in (10, 20)')
FROM dual;
DBMS_XMLQUERY.GETXML('SELECTDEPTNO'||'CURSORIS(SELECTEMPNOFROMEMPEWHEREE.DEPT=D.
<?xml version = '1.0'?>
<ERROR>oracle.xml.sql.OracleXMLSQLException: ORA-00923
: FROM keyword not found where expected
</ERROR
THIS IS DUE TO THE CURSOR AND ITS FROM STATEMENT?
CAN ANY ONE PLEASE GUIDE AS TO HOW TO USE CURSORS IN A SELECT STATEMENT PLEASE?Another duplicate thread. See my response Select CLAUSE error using CURSORS & XSU.Please SEE..
Cheers, APC -
Cursor - dynamic select statement?
Hello!
I am passing a string into a function, and my string is a sql select statement. Is it possible to create a cursor using the string that is passed to the function?
Ex:
create or replace function "GETCOUNT"
(sqlstring in VARCHAR2)
return NUMBER
is
rec_count NUMBER; --I eventually want to return a count of the rows returned from my sqlstring
cursor c1 is sqlstring <--this is what I'd like to do...but how?
begin
... -- I haven't gotten to this point yet
end;
The error I get says 'Encountered the symbol "sqlstring" when expecting on of the following: ( select '
Thanks in advance!!Something like this ->
create or replace procedure gen_cur(str in varchar2,rc in out sys_refcursor)
is
str1 varchar2(500);
begin
str1 := 'select empno,ename,hiredate,sal
from emp
where '||str;
open rc for str1;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
variable b refcursor
declare
src varchar2(300);
begin
src:= 'sal between 2000 and 7000';
gen_cur(src,:b);
exception
when others then
dbms_output.put_line(sqlerrm);
end;
print bRegards.
Satyaki De. -
Cursor Expression Select Statement
SELECT DNAME, CURSOR(SELECT SAL,COMM FROM EMP E WHERE E.DEPTNO = D.DEPTNO) FROM DEPT D
DNAME CURSOR(SELECTSAL,COM
ACCOUNTING CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
SAL COMM
2450
5000
1300
RESEARCH CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
SAL COMM
800
2975
3000
1100
3000
SALES CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
SAL COMM
1600 300
1250 500
1250 1400
2850
1500 0
950
6 rows selected.
OPERATIONS CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
no rows selected
Can we get rid of "CURSOR STATEMENT : 2"(Also I don't know what is its
significance there) from the output so that the output is in a readable format.
Thanks,
Neeraj GoelI want Data in the Format Like
deptno cnt1
cnt2
cntN
deptno cnt1
cntM
when you have a parent table (dept) and you sometimes
want to see the child rows (emp) but "on demand". For example, you fetch
"select deptno, cursor( from emp ) from dept" and the user says "hmmm, I'd like
to see the emps in deptno 20" -- great, you just fetch that cursor and display
them -- you didn't have to bring everyone else back with that query.
Also useful when you have more then 1:M relationship in a query
Now you can get a result set like:
deptno Sal Comm
Sal Comm
Sal
3 Sal, 2 Comm items and one deptno -- in one "row"
Thats why i used cursor Expression.
Thanks
Neeraj Goel -
CURSOR Inside select statement
Hi,
I am trying to generate reports in XML Publisher using PL/SQL Procedure. Following is my procedure:
PROCEDURE XXDL_PO_VEN_SUMMARY_BY_VEN (ERR_BUFF VARCHAR2,
RETCODE VARCHAR2,
in_vendor_number IN VARCHAR2,
in_start_date IN VARCHAR2,
in_end_date IN VARCHAR2
IS
SQL_STMT VARCHAR2(15000);
result CLOB;
in_vendor_summary_from_date DATE;
in_vendor_summary_to_date DATE;
CURSOR_WHERE_CLAUSE VARCHAR2(1000);
BEGIN
in_vendor_summary_from_date := TO_DATE(in_start_date,'YYYY/MM/DD HH24:MI:SS');
in_vendor_summary_to_date := TO_DATE(in_end_date,'YYYY/MM/DD HH24:MI:SS');
--DBMS_OUTPUT.PUT_LINe('start date object'||in_vendor_summary_from_date);
--DBMS_OUTPUT.PUT_LINe('end date object'||in_vendor_summary_to_date);
IF in_start_date IS NOT NULL AND LENGTH(in_start_date) > 0 AND in_end_date IS NOT NULL AND LENGTH(in_end_date) > 0 THEN
CURSOR_WHERE_CLAUSE := 'AND TO_DATE(CONCAT(CONCAT(SUMMARY_MONTH, ''/''), SUMMARY_YEAR),''MM/YYYY'') BETWEEN'
||' '''||in_vendor_summary_from_date||''' AND '''||in_vendor_summary_to_date||'''';
END IF;
IF in_vendor_number IS NOT NULL AND LENGTH(in_vendor_number)>0 THEN
CURSOR_WHERE_CLAUSE := CURSOR_WHERE_CLAUSE || ' AND UPPER(VENDOR_NUM) LIKE UPPER('''||in_vendor_number||'%'')';
END IF;
--DBMS_OUTPUT.PUT_LINe('cursor where clause : '||CURSOR_WHERE_CLAUSE );
SQL_STMT := 'SELECT VS.VENDOR_NUM,' ||
' VS.VENDOR_NAME,' ||
' VS.SUMMARY_MONTH,' ||
' VS.SUMMARY_YEAR,' ||
' VS.ORD_TOTAL_CNT,' ||
' VS.ORD_DOLLAR_AMT,'||
' VS.RPO_TOTAL_CNT,' ||
' VS.RPO_DOLLAR_AMT,'||
' VS.CWA_TOTAL_CNT,' ||
' VS.CWA_DOLLAR_AMT,'||
' VS.STD_TOTAL_CNT,' ||
' VS.STD_DOLLAR_AMT,'||
' VS.BLM_TOTAL_CNT,' ||
' VS.BLM_DOLLAR_AMT,'||
' VS.INVOICE_TOTAL_CNT,' ||
' VS.INVOICE_DOLLAR_AMT, ' ||
' CURSOR (SELECT nvl(SUM( nvl(VS1.ORD_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS1 '||
' ) TOTAL_ORDERS, ' ||
' CURSOR(SELECT nvl(SUM(nvl(VS2.ORD_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS2 '||
' ) TOTAL_DOLLARS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS3.RPO_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS3 '||
' ) RPO_ORDERS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS4.RPO_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS4 '||
' ) RPO_DOLLARS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS5.CWA_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS5 '||
' ) CWA_ORDERS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS6.CWA_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS6 '||
' ) CWA_DOLLARS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS7.STD_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS7 '||
' ) STD_ORDERS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS8.STD_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS8 '||
' ) STD_DOLLARS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS9.BLM_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS9 '||
' ) BLM_ORDERS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS10.BLM_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS10 '||
' ) BLM_DOLLARS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS11.INVOICE_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS11 '||
' ) TOTAL_INVOICES,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS12.INVOICE_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS12 '||
' ) INVOICE_DOLLARS, ' ||
' CURSOR(SELECT nvl(SUM(nvl(V1.RPO_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V1.RPO_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V1.RPO_TOTAL_CNT,0))) FROM '|| ' STANPRS.VENDOR_SUMMARY V1 ) RPO_ORDERS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V2.RPO_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V2.RPO_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V2.RPO_DOLLAR_AMT,0))) '|| 'FROM STANPRS.VENDOR_SUMMARY V2 ) RPO_DOLLARS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V3.CWA_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V3.CWA_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V3.CWA_TOTAL_CNT,0))) FROM '|| 'STANPRS.VENDOR_SUMMARY V3 ) CWA_ORDERS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V4.CWA_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V4.CWA_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V4.CWA_DOLLAR_AMT,0))) '||
'FROM STANPRS.VENDOR_SUMMARY V4 ) CWA_DOLLARS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V5.STD_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V5.STD_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V5.STD_TOTAL_CNT,0))) FROM '|| 'STANPRS.VENDOR_SUMMARY V5 ) STD_ORDERS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V6.STD_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V6.STD_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V6.STD_DOLLAR_AMT,0))) '||
'FROM STANPRS.VENDOR_SUMMARY V6 ) STD_DOLLARS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V7.BLM_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V7.BLM_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V7.BLM_TOTAL_CNT,0))) '||
'FROM STANPRS.VENDOR_SUMMARY V7 ) BLM_ORDERS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V8.BLM_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V8.BLM_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V8.BLM_DOLLAR_AMT,0))) '||
'FROM STANPRS.VENDOR_SUMMARY V8 ) BLM_DOLLARS_PER,' ||
'CURSOR(SELECT NVL(SUM(nvl(V9.INVOICE_TOTAL_CNT,0)),0)*100 / '||
'decode(SUM(nvl(V9.INVOICE_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V9.INVOICE_TOTAL_CNT,0))) FROM '||
'STANPRS.VENDOR_SUMMARY V9 ) TOTAL_INVOICES_PER, ' ||
'CURSOR(SELECT NVL(SUM(nvl(V10.INVOICE_DOLLAR_AMT,0)),0)*100/ decode(SUM(nvl(V10.INVOICE_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V10.INVOICE_DOLLAR_AMT,0))) FROM STANPRS.VENDOR_SUMMARY V10 ) INVOICE_DOLLARS_PER' ||
' FROM STANPRS.VENDOR_SUMMARY VS' ||
' WHERE';
--DBMS_OUTPUT.PUT_LINE('STANPRS.VENDOR_SUMMARY V10 WHERE V10.VENDOR_NUM = VS.VENDOR_NUM ') INVOICE_DOLLARS_PER');
IF in_start_date IS NOT NULL AND LENGTH(in_start_date) > 0 AND in_end_date IS NOT NULL AND LENGTH(in_end_date) > 0 THEN
SQL_STMT := SQL_STMT || ' TO_DATE(CONCAT(CONCAT(VS.SUMMARY_MONTH, ''/''), VS.SUMMARY_YEAR),''MM/YYYY'') BETWEEN '
||' '''||in_vendor_summary_from_date||''' AND '''||in_vendor_summary_to_date||'''';
END IF;
IF in_vendor_number IS NOT NULL AND LENGTH(in_vendor_number)>0 THEN
IF SUBSTR(SQL_STMT, LENGTH(SQL_STMT)-4) = 'WHERE' THEN
SQL_STMT := SQL_STMT || ' UPPER(VS.VENDOR_NUM) LIKE UPPER('''||in_vendor_number||'%'')';
ELSE
SQL_STMT := SQL_STMT || ' AND UPPER(VS.VENDOR_NUM) LIKE UPPER('''||in_vendor_number||'%'')';
END IF;
END IF;
IF in_vendor_name IS NOT NULL AND LENGTH(in_vendor_name)>0 THEN
IF SUBSTR(SQL_STMT, LENGTH(SQL_STMT)-4) = 'WHERE' THEN
SQL_STMT := SQL_STMT || ' UPPER(VS.VENDOR_NAME) LIKE UPPER('''||in_vendor_name||'%'')';
ELSE
SQL_STMT := SQL_STMT || ' AND UPPER(VS.VENDOR_NAME) LIKE UPPER('''||in_vendor_name||'%'')';
END IF;
END IF;
IF SUBSTR(SQL_STMT, LENGTH(SQL_STMT)-4) = 'WHERE' THEN
SQL_STMT := SQL_STMT || ' VS.VENDOR_NUM = '''||in_vendor_number||'''';
END IF; */
SQL_STMT := SQL_STMT || ' ORDER BY VS.VENDOR_NUM, VS.VENDOR_NAME ASC';
--insert into XX_ERROR VALUES('Spriha',SQL_STMT);
SELECT DBMS_XMLGEN.GETXML(SQL_STMT) INTO result FROM DUAL;
FND_FILE.PUT_LINE(FND_FILE.OUTPUT, result);
EXCEPTION
WHEN OTHERS THEN
FND_FILE.PUT_LINE(FND_FILE.OUTPUT, SQLERRM);
END XXDL_PO_VEN_SUMMARY_BY_VEN ;
Now u can see in this Procedure that i have created so many cursors. i.e. for one function i have created one cursor. at present thesecursors are working fine. but the time i will put the where clause inside the cursors this will throw an ERROR ' ORA-01460 Unimplemented or Unreasonable Conversion Request'. Also these cursors are working with where clause but they are not working together. i.e when i will remove some cursors then i am not getting any error but at present i.e. without any where clause they all r working fine. Seriously i am not getting anything that what is happening. Can anyone help me? waiting for the reply....
Thanks
SprihaThis is pretty much a poster for ugly code.
Simplify this down to the bare minimum and debug it. Then add back the rest of the functionality.
But I am at a loss, looking at this code, as to why any cursor is either desirable or required. -
How can I iterate over the columns of a REF CURSOR?
I have the following situation:
DECLARE
text VARCHAR2 (100) := '';
TYPE gen_cursor is ref cursor;
c_gen gen_cursor;
CURSOR c_tmp
IS
SELECT *
FROM CROSS_TBL
ORDER BY sn;
BEGIN
FOR tmp IN c_tmp
LOOP
text := 'select * from ' || tmp.table_name || ' where seqnum = ' || tmp.sn;
OPEN c_gen FOR text;
-- here I want to iterate over the columns of c_gen
-- c_gen will have different number of columns every time,
-- because we select from a different table
-- I have more than 500 tables, so I cannot define strong REF CURSOR types!
-- I need something like
l := c_gen.columns.length;
for c in c_gen.columns[1]..c_gen.columns[l]
LOOP
-- do something with the column value
END LOOP;
END LOOP;
END;As you can see from the comments in the code, I couln'd find any examples on the internet with weak REF CURSORS and selecting from many tables.
What I found was:
CREATE PACKAGE admin_data AS
TYPE gencurtyp IS REF CURSOR;
PROCEDURE open_cv (generic_cv IN OUT gencurtyp, choice INT);
END admin_data;
CREATE PACKAGE BODY admin_data AS
PROCEDURE open_cv (generic_cv IN OUT gencurtyp, choice INT) IS
BEGIN
IF choice = 1 THEN
OPEN generic_cv FOR SELECT * FROM employees;
ELSIF choice = 2 THEN
OPEN generic_cv FOR SELECT * FROM departments;
ELSIF choice = 3 THEN
OPEN generic_cv FOR SELECT * FROM jobs;
END IF;
END;
END admin_data;
/But they have only 3 tables here and I have like 500. What can I do here?
Thanks in advance for any help!The issue here is that you don't know your columns at design time (which is generally considered bad design practice anyway).
In 10g or before, you would have to use the DBMS_SQL package to be able to iterate over each of the columns that are parsed from the query... e.g.
CREATE OR REPLACE PROCEDURE run_query(p_sql IN VARCHAR2) IS
v_v_val VARCHAR2(4000);
v_n_val NUMBER;
v_d_val DATE;
v_ret NUMBER;
c NUMBER;
d NUMBER;
col_cnt INTEGER;
f BOOLEAN;
rec_tab DBMS_SQL.DESC_TAB;
col_num NUMBER;
v_rowcount NUMBER := 0;
BEGIN
-- create a cursor
c := DBMS_SQL.OPEN_CURSOR;
-- parse the SQL statement into the cursor
DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
-- execute the cursor
d := DBMS_SQL.EXECUTE(c);
-- Describe the columns returned by the SQL statement
DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
-- Bind local return variables to the various columns based on their types
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000); -- Varchar2
WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val); -- Number
WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val); -- Date
ELSE
DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000); -- Any other type return as varchar2
END CASE;
END LOOP;
-- Display what columns are being returned...
DBMS_OUTPUT.PUT_LINE('-- Columns --');
FOR j in 1..col_cnt
LOOP
DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' - '||case rec_tab(j).col_type when 1 then 'VARCHAR2'
when 2 then 'NUMBER'
when 12 then 'DATE'
else 'Other' end);
END LOOP;
DBMS_OUTPUT.PUT_LINE('-------------');
-- This part outputs the DATA
LOOP
-- Fetch a row of data through the cursor
v_ret := DBMS_SQL.FETCH_ROWS(c);
-- Exit when no more rows
EXIT WHEN v_ret = 0;
v_rowcount := v_rowcount + 1;
DBMS_OUTPUT.PUT_LINE('Row: '||v_rowcount);
DBMS_OUTPUT.PUT_LINE('--------------');
-- Fetch the value of each column from the row
FOR j in 1..col_cnt
LOOP
-- Fetch each column into the correct data type based on the description of the column
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||v_v_val);
WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||v_n_val);
WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'));
ELSE
DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||v_v_val);
END CASE;
END LOOP;
DBMS_OUTPUT.PUT_LINE('--------------');
END LOOP;
-- Close the cursor now we have finished with it
DBMS_SQL.CLOSE_CURSOR(c);
END;
SQL> exec run_query('select empno, ename, deptno, sal from emp where deptno = 10');
-- Columns --
EMPNO - NUMBER
ENAME - VARCHAR2
DEPTNO - NUMBER
SAL - NUMBER
Row: 1
EMPNO : 7782
ENAME : CLARK
DEPTNO : 10
SAL : 2450
Row: 2
EMPNO : 7839
ENAME : KING
DEPTNO : 10
SAL : 5000
Row: 3
EMPNO : 7934
ENAME : MILLER
DEPTNO : 10
SAL : 1300
PL/SQL procedure successfully completed.
SQL> exec run_query('select * from emp where deptno = 10');
-- Columns --
EMPNO - NUMBER
ENAME - VARCHAR2
JOB - VARCHAR2
MGR - NUMBER
HIREDATE - DATE
SAL - NUMBER
COMM - NUMBER
DEPTNO - NUMBER
Row: 1
EMPNO : 7782
ENAME : CLARK
JOB : MANAGER
MGR : 7839
HIREDATE : 09/06/1981 00:00:00
SAL : 2450
COMM :
DEPTNO : 10
Row: 2
EMPNO : 7839
ENAME : KING
JOB : PRESIDENT
MGR :
HIREDATE : 17/11/1981 00:00:00
SAL : 5000
COMM :
DEPTNO : 10
Row: 3
EMPNO : 7934
ENAME : MILLER
JOB : CLERK
MGR : 7782
HIREDATE : 23/01/1982 00:00:00
SAL : 1300
COMM :
DEPTNO : 10
PL/SQL procedure successfully completed.
SQL> exec run_query('select * from dept where deptno = 10');
-- Columns --
DEPTNO - NUMBER
DNAME - VARCHAR2
LOC - VARCHAR2
Row: 1
DEPTNO : 10
DNAME : ACCOUNTING
LOC : NEW YORK
PL/SQL procedure successfully completed.
SQL>From 11g onwards, you can create your query as a REF_CURSOR, but then you would still have to use the DBMS_SQL package with it's new functions to turn the refcursor into a dbms_sql cursor so that you can then describe the columns in the same way.
http://technology.amis.nl/blog/2332/oracle-11g-describing-a-refcursor
Welcome to the issues that are common when you start to attempt to create dynamic code. If your design isn't specific then your code can't be either and you end up creating more work in the coding whilst reducing the work in the design. ;) -
Sorting the results returned by a Ref cursor
Hi All,
I have a scenario where i am asked to sort results returned by a ref cursor.
I have to pass the column to be sorted as the Input parameter to a stored procedure. I tried using 'order by sorting_parameter' in the ref cursor's select query, but it is not sorting the results. It is not throwing any error even.
Please help.
Many Thanks...Hi
i came across the below reply for a thread with the similar query as above.
<<
Justin Cave
Posts: 10,696
From: Michigan, USA
Registered: 10/11/99
Re: sort data in ref cursor
Posted: Feb 3, 2005 10:30 AM in response to: [email protected] Reply
No. You could sort the data in the SQL statement from which the REF CURSOR was created, but once you have a REF CURSOR, you cannot do anything but fetch from it.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
>>
So, the results from a ref cursor cant be sorted?? There is no way out?
Kindly advise. -
Hi All,
I am having a REF cursor returned by a function which contains a CURSOR with in its select statement. Can any one please let me know how to fetch these values into variable for further processing. A sample query for my REF CURSOR is
Select dept_id,
cursor(
select emp_id, emp_name
from emp e
where e.dept_id = d.dept_id)
from dept dSQL> l
1 create function fun
2 return sys_refcursor
3 is
4 retval sys_refcursor;
5 begin
6 open retval for select object_id
7 , cursor (select dummy
8 from dual
9 )
10 from all_objects
11 where rownum <= 10;
12 return retval;
13* end fun;
SQL>
SQL>
SQL>
SQL> var rc refcursor
SQL> exec :rc := fun
PL/SQL procedure successfully completed.
SQL> print :rc
OBJECT_ID CURSOR(SELECTDUMMYFR
20 CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
D
51 CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
D
X
26 CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
D
X
10 rows selected.
SQL> -
Ref.Cursor Problem - URGENT
Hi Friends,
For my report ( calc.rdf ) ,
I am passing two input parameters P_Client_ID, P_Plan_ID.
In .rdf, I had a ref.cursor for selecting records based on
two input parameters values.
case1:
P_client_ID = 'SRINI'
P_Plan_ID = '3232'
My report is generating the output for plan 3232.
case2:
P_client_ID = 'SRINI'
P_Plan_ID = NULL
My report is generating the output for all plans.
case3:
P_client_ID = 'SRINI'
P_Plan_ID = '3232,3257,3259'
My report is not generating any output here.
Only blanck page i am getting.
How can i pass multiple plans to ref.cursor at a time ?
Note:
I now, we cannot make Lexical references in a PL/SQL statement.
Any other way to solve this problem ?
Thanks for Help,
sriniI think that you work with 'static' ref cursor.
From Oracle 8.1.5, we can use 'dynamic' ref cursors.
With 'dynamic' ref cursor we can avoid the use of lexical parameters in Reports 3.0 / 6i.
With 'static' ref cursor this is not possible in all cases.
For example, if we need dynamic WHERE, we practically can't use 'static' ref cursor.
Example for 'dynamic' ref cursor (dynamic WHERE)
1. Stored package
CREATE OR REPLACE PACKAGE report_dynamic IS
TYPE type_ref_cur_sta IS REF CURSOR RETURN dept%ROWTYPE; -- for Report Layout only
TYPE type_ref_cur_dyn IS REF CURSOR;
FUNCTION func_dyn (p_where VARCHAR2) RETURN type_ref_cur_dyn;
END;
CREATE OR REPLACE PACKAGE BODY report_dynamic IS
FUNCTION func_dyn (p_where VARCHAR2) RETURN type_ref_cur_dyn IS
l_ref_cur_dyn type_ref_cur_dyn;
BEGIN
OPEN l_ref_cur_dyn FOR
'SELECT * FROM dept WHERE ' || NVL (p_where, '1 = 1');
RETURN l_ref_cur_dyn;
END;
END;
2.2 Query PL/SQL in Reports
function QR_1RefCurQuery return report_dynamic.type_ref_cur_sta is
begin
return report_dynamic.func_dyn (:p_where);
end;
Note that Oracle Reports 3.0 / 6i needs 'static' ref cursor type for building Report Layout.
So, in package specification we must have both ref cursor types, static for Report Layout
and dynamic for ref cursor query.
Regards
Zlatko Sirotic -
How to return a ref cursor from this dbms_sql?
Hi,
Can anyone show me how to return a ref cursor from this dbms_sql based procedure? I see 11g has a dbms_sql.to_refcursor(cursor_handle). How can this be done is 10g?
Thx.
CREATE OR REPLACE PROCEDURE Sample_Get_t
p_sample_id sample.sample_id%TYPE,
p_contract_id sample.contr_id%TYPE
IS
cursor_handle INT;
sql_stmnt varchar2(500);
rows_processed NUMBER;
BEGIN
sql_stmnt :=
'SELECT
sample_id,
contr_id,
rcpt_id
FROM
sample s
WHERE
s.contr_id = :1
and s.sample_id = :2
ORDER BY
sample_id';
cursor_handle := dbms_sql.open_cursor;
dbms_sql.parse(cursor_handle, sql_stmnt, dbms_sql.native);
dbms_sql.bind_variable(cursor_handle, ':1', p_contract_id);
dbms_sql.bind_variable(cursor_handle, ':2', p_sample_id);
rows_processed := dbms_sql.execute(cursor_handle);
dbms_sql.close_cursor(cursor_handle);
END Sample_Get_t;In 10 this cannot be done with dbms_sql (to my knowledge). There are a couple of other options.
1) open the ref cursor for the dynamic statement using bind variables (or SYS_CONTEXT variables, which i prefer since they are much easier to deal with when you are dynamically adding predicates).
declare
wRefCursor SYS_REFCURSOR;
begin
open wRefCursor for 'select * from all_objects where owner = :Logged_in_user' using user;
end;
/or using the context (the context will bind for you)
declare
wRefCursor SYS_REFCURSOR;
begin
open wRefCursor for 'select * from all_objects where owner = SYS_CONTEXT(''CONTEXT_NAME'', ''VARIABLE_NAME'') ';
end;
/Be aware that contexts ALWAYS return varchar values, so if you are comparing to a number you should wrap it in TO_NUMBER, a date, TO_DATE and so on....
2) change the DBMS_SQL to do an insert into a global temporary table and return the ref cursor which select's * from GTT;
3) upgrade to Oracle 11 :) -
Ref cursor argument in where clause
Env: ORCL 9.2
I have a func that uses the parameters in a where clause and returns a ref cursor as result. That works fine.
I want to create an overloaded func that replaces one argument with a ref cursor. (instead of accepting a single value as an argument I want to accept multiple values) Can you specify the ref cursor in a where clause with out looping through the cursor ?
CURRENT
func(arg1,arg2,arg3) returns ref cursor
is
select blah from sometable s
where s.a = arg1
and s.b = arg2
and s.c = arg3
NEW
func(ref_cur_arg1,arg2,arg3) returns ref cursor
is
select blah from sometable s
where s.a = ref_cur_arg1
and s.b = arg2
and s.c = arg3
is there something like:
where s.a in (loop fetch ref_cur_arg1 end loop)
thxThanks Rich,
That's pretty much what I came up with:
FUNCTION f_bond_price_w_bb_stat (
p_id_ref gtyp_instr_id_ref,
p_price_srce bond_price.PRICE_SRCE%type,
p_price_type bond_price.PRICE_TYPE%type,
p_price_date bond_price.PRICE_DATE%type)
RETURN gtyp_bondprice_w_bb_stat_rfc
IS
lv_bondprice_rfc gtyp_bondprice_w_bb_stat_rfc;
TYPE ARRAY1 IS TABLE OF instr_ext_id_map.ext_id_value%TYPE INDEX BY BINARY_INTEGER;
t_instr_id ARRAY1;
instr_ids INSTR_EXT_ID_T := INSTR_EXT_ID_T();
BEGIN
--suck the contents of the ref cursor into a local virtual tmp table
FETCH p_id_ref BULK COLLECT INTO t_instr_id;
FOR i IN 1..t_instr_id.COUNT LOOP
instr_ids.extend;
instr_ids(instr_ids.count) := t_instr_id(i);
END LOOP;
CLOSE p_id_ref;
OPEN lv_bondprice_rfc FOR
SELECT
bs.ID_ISIN,
bs.TICKER,
bs.CPN,
bs.MATURITY,
round(months_between(bs.MATURITY,sysdate)/12,1),
bs.ISSUER_INDUSTRY,
bs.INDUSTRY_SECTOR,
FROM bond_price b,
instr_ext_id_map ext,
etl.mdy_ratingstatic mrs,
etl.mdy_extid mxid,
etl.bloomberg_static bs
WHERE b.INSTR_ID = ext.instr_id
AND bs.ID_ISIN(+) = ext.ext_id_value
AND bs.ID_ISIN = mxid.EXTIDVALUE(+)
AND mrs.MOODYDEBTNUM(+) = mxid.MOODYDEBTNUM
AND ext.ext_id_value in (select * from TABLE (cast (instr_ids AS INSTR_EXT_ID_T) ))
AND b.PRICE_SRCE = p_price_srce
AND b.PRICE_TYPE = p_price_type
AND b.PRICE_DATE = p_price_date
RETURN lv_bondprice_rfc;
END f_bond_price_w_bb_stat; -
What are the advantages to using a REF cursor
When would I want to use a REF cursor?
REF CURSORs are not cursors, they're references to cursors. This means they're dynamic. So you can completely change the query executed by the cursor.
For example you can use a REF CURSOR to select from one of a number of tables (depending on an input parameter or other sort of flag). The results are fetched into the same resultset holder and hence you only need a single lot of code to process the results.
Also you can also pass a REF CURSOR as a argument to other PL/SQL procedures and functions.
rgds, APC -
Example of Simple SP using REF Cursors
Hi All,
i want simple stored procedure using REF Cursors.
Select * From Emp Where Deptno='20'
Use Scott & Tiger, Default database.
i am poassing parameter deptno at runtime.i want all the records of deptno=20.
i know REF Cursors return Record Set.but i created,it will throw errors.
Anyone pls helpPost your code what you have tried. Also your requirement can be fulfilled with parametrized cursor.
http://download.oracle.com/docs/cd/E11882_01/appdev.112/e17126/static.htm#LNPLS00602
http://download.oracle.com/docs/cd/E11882_01/appdev.112/e17126/static.htm#LNPLS00605 -
Using ref cursor in "in clause" in select statement
Hi,
Is there any way can we use the ref cursor in the in condition of a select statement.
Regards,
Venkat.
Edited by: ramanamadhav on Aug 23, 2011 11:14 AMramanamadhav wrote:
I'm sorry if I post in confusing way. I will give an example. Just see the psudo code here.
declare
rf_cur sys_refcursor;
begin
pr_test(empno,rf_cur);
-- rf_cur returning emp names.
select * from emp
where empname in (ref_cusor results);-- here i want to consume my ref cursor result in the in conditions.
end;
Thanks &Regards,
Venkat.No you can't do that. A ref cursor is not a set of results as you believe.
Take a read of this article...
{thread:id=886365}
Maybe you are looking for
-
Faster bus and bigger cache in the 3.0 GHz?
In Bare Feats test (http://www.barefeats.com/quad06.html) of the Mac Pro 3.0 they say "The 3.0GHz Mac Pro's 20% faster clock speed, larger L2 cache, and faster system bus enables it to muscle through these two non-optimized apps." Hopefully I'm misre
-
Why does my snapchat keep force closing?
my snapchat keeps closing everytime i try to open it i already delelted it and tried to re-install it but its not working, any suggestions?
-
Nach der Umstellung auf Adobe CC Anfang des Jahres, haben unsere Aktionen teilweise nicht mehr funktioniert, weil die Softwareentwickler scheinbar der Meinung waren "Ebene Kopie" in "Ebene kopieren" umzubenennen. Wir haben daher unsere Aktionen (teil
-
Possible Liquid Bug: Moving data into e-commerce layout
Liquid will not seemingly pass any data from outside an e-commerce layout into an e-commerce layouts is intentional or a bug. If you create an include place it in a template then try to pass any data ie assign into any ecommerce layout it does not wo
-
Pearl and I want it to autmaically connect
Simple deal here, i want to set my phone by my computer and it to sync whithought me having to do interaction on the computer. What am I missing here? Thanks, Mike