Ref cursor type
Hello All,
I have a small procedure like the following.
CREATE OR REPLACE procedure PROC_DEPT_SALE(SALEC_CUR out sys_refcursor)
as
begin
open sale for select * from dept_sale where dept_no='Z2341324';
end;
I just want to execute it from toad like
exec PROC_DEPT_SALE( C1);
In order to do that i need to have a sys_refcursor c1 created in my DB. I know that i can execute it as a plsql block like this
DECLARE
SALE SYS_REFCURSOR;
BEGIN
PROC_DEPT_SALE( SALE );
END;
Could anyone please let me know how can i create a TYPE REF cursor which is permanantly stored in the DB and then execute the proc the way i wanted to using the first statement.
Thanks
Actually, you had it correct with
CREATE OR REPLACE package cur_type as
TYPE ref_cur IS REF CURSOR;
end;but you still need to declare a variable of the type to pass the the procedure since the procedure needs to have a cursor variable to open. Given your package, the call would be more like:
DECLARE
l_cur cur_type.ref_cur;
BEGIN
proc_dept_sale(l_cur);
<do something with l_cur>
END;Forget about the cursor for a minute and think through this example. If I have a procedure that looks like:
CREATE PROCEDURE get_name (p_id IN NUMBER,
p_name OUT VARCHAR2) AS
BEGIN
SELECT last_name||', '||first_name
INTO p_name
FROM employee
WHERE emp_id = p_id;
END;I can pass a literal value to the procedure for the p_id parameter (e.g. 42) or I could pass a variable holding a value.
What do I need to pass to the procedure in p_name to get the name back out? A procedure returning a cursor is no different than this in concept.
John
Similar Messages
-
This is the first time I have a need at work to use a REF CURSOR type of the weak variety. After reading documentation
in SF's 'bible' Oracle PL/SQL Programming, I did everything right. Here's the code snippet:
-- TYPES
TYPE content_ID_curtype IS
REF CURSOR;
-- VARIABLES
c_SEARCH_STRING CONSTANT VARCHAR2(12) := 'v_content_id';
content_ID_cur content_ID_curtype;
v_content_ID am_content_content.content_ID%TYPE;
BEGIN
cache_sql_rec.sql_stmt := REPLACE(cache_sql_rec.sql_stmt, c_SEARCH_STRING, template_name_rec.content_ID);
OPEN content_ID_cur FOR cache_sql_rec.sql_stmt;
LOOP
FETCH content_ID_cur
INTO v_content_ID;
EXIT WHEN content_ID_cur%NOTFOUND;
END LOOP;
CLOSE content_ID_cur;
END;
Now the error I get is.... ORA-00911: invalid character When I used DBMS_OUTPUT to see the actual value of
"cache_sql_rec.sql_stmt", the SQL query looks fine. Even when I hardcoded the sql statement after the FOR keyword, it
worked fine. It's only when I use a variable to hold the whole SQL statement does it fail. Here's one value for the
variable cache_sql_rec.sql_stmt:
SELECT b.content_id
FROM am_content_mofcollection a,
am_content_collection b
WHERE a.content_id = 149090
AND a.collection_id = b.collection_id;
Basically I replace the string with an actual content_ID. Now content_ID is of type NUMBER(12) as is the variable
v_content_ID declared so that I FETCH INTO that variable, but the problem is, is that the exception gets raised during
the OPEN...cursor...FOR....sql_statement command.
Any thoughts on this bug?
Thanks,
Gio
Giovanni Jaramillo
Senior Software Engineer
Oracle Database Group
Amplified Holdings, Inc.
5750 Wilshire Blvd., Ste 501
Los Angeles, CA 90036-3638
(323)-556-8792
[email protected] http://www.amplified.comYes it turns out that the data had a semicolon at the end since it's being inserted by someone else. I know when executing DDL or DML statements via NDS you omit the semicolon. But didn't know it applied to REF CURSORS.
Also I added a colon to the variable that I was REPLACING since it can act as a bind variable.
Thanks Andrew.
Gio
null -
Stored PL/SQL function that returns REF CURSOR type
Hello everyone,
I've come through the following problem:
1.- I created an PL/SQL stored procedure which returns a REF CURSOR element, definition looks like this:
PACKAGE PKG_LISTADOS AS
TYPE tuplas IS REF CURSOR;
/* Procedimientos exportados por el paquete */
PROCEDURE inicializarModuloListados;
FUNCTION recaudacionUltimoMes(medioPago DEF_MEDIO_PAGO.MEDIO_PAGO%TYPE)
RETURN tuplas;
2.- Now I would like to call the stored procedure and retrieve the PL/SQL cursor as a ResultSet Java Object. The code I wrote is this:
Connection conn;
XmlDocument paramDef;
conn=poolMgr.getConnection str_poolDBConnection);
try
CallableStatement cstmt=conn.prepareCall("{?=call PKG_LISTADOS.recaudacionUltimoMes(?)}");
cstmt.registerOutParameter(1, java.sql.Types.OTHER);
cstmt.setString(2, "MONEDA");
cstmt.executeQuery();
ResultSet rs=(ResultSet)cstmt.getObject(1);
catch(SQLException sqlE)
3.- However, I can't make it OK, all the time I get the following error:
SQL Error(17004), java.sql.SQLException: Non valid column type
May anyone help me with this, thanks in advance:
Miguel-Angel<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by angelrip:
Hello everyone,
I've come through the following problem:
1.- I created an PL/SQL stored procedure which returns a REF CURSOR element, definition looks like this:
PACKAGE PKG_LISTADOS AS
TYPE tuplas IS REF CURSOR;
/* Procedimientos exportados por el paquete */
PROCEDURE inicializarModuloListados;
FUNCTION recaudacionUltimoMes(medioPago DEF_MEDIO_PAGO.MEDIO_PAGO%TYPE)
RETURN tuplas;
2.- Now I would like to call the stored procedure and retrieve the PL/SQL cursor as a ResultSet Java Object. The code I wrote is this:
Connection conn;
XmlDocument paramDef;
conn=poolMgr.getConnection str_poolDBConnection);
try
CallableStatement cstmt=conn.prepareCall("{?=call PKG_LISTADOS.recaudacionUltimoMes(?)}");
cstmt.registerOutParameter(1, java.sql.Types.OTHER);
cstmt.setString(2, "MONEDA");
cstmt.executeQuery();
ResultSet rs=(ResultSet)cstmt.getObject(1);
catch(SQLException sqlE)
3.- However, I can't make it OK, all the time I get the following error:
SQL Error(17004), java.sql.SQLException: Non valid column type
May anyone help me with this, thanks in advance:
Miguel-Angel<HR></BLOCKQUOTE>
Do something like the following:
cstmt = conn.prepareCall("{call customer_proc(?, ?)}");
//Set the first parameter
cstmt.setInt(1, 40);
//Register to get the Cursor parameter back from the procedure
cstmt.registerOutParameter(2, OracleTypes.CURSOR);
cstmt.execute();
ResultSet cursor = ((OracleCallableStatement)cstmt).getCursor(2);
while(cursor.next())
System.out.println("CUSTOMER NAME: " + cursor.getString(1));
System.out.println("CUSTOMER AGE: " + cursor.getInt(2));
cursor.close();
null -
I have two packages (please see below). A procedure from the first package (TEST1) calls a procedure in the second package (TEST2), which has an output parameter of REF CURSOR TYPE.
I am getting an error at compile time, in the calling procedure.
Can anyone please help on finding out what am I missing here?
Thank you in advance.
- Ketan Bhuptani
Here are the procedures:
(1)
CREATE OR REPLACE PACKAGE TEST1
AS
TYPE cursor_type_pass IS REF CURSOR;
PROCEDURE call_cursor (result_flag OUT varchar2);
END TEST1;
CREATE OR REPLACE PACKAGE BODY TEST1
AS
PROCEDURE call_cursor (result_flag OUT varchar2) IS
v_salary int;
proc_cursor cursor_type_pass;
CURSOR emp_cur is select empid from emp;
BEGIN
FOR emp_cur_var IN emp_cur
LOOP
test2.open_cursor(emp_cur_var.empid, proc_cursor);
v_salary := proc_cursor.salary;
-- getting an error Invalid reference to variable proc_cursor at this line
END LOOP;
END call_cursor;
END TEST1;
(2)
CREATE OR REPLACE PACKAGE TEST2
AS
TYPE cursor_type_return IS REF CURSOR;
PROCEDURE open_cursor (emp_id IN varchar2, out_cur OUT cursor_type_return);
END TEST2;
CREATE OR REPLACE PACKAGE BODY TEST2
AS
PROCEDURE open_cursor (emp_id IN varchar2, out_cur OUT cursor_type_return) IS
BEGIN
OPEN out_cur FOR
select location, salary from emp_information where empid = emp_id;
END open_cursor;
END TEST2;
create table emp_information
(location varchar2(30), salary int, empid varchar2(10));
create table emp
(empid varchar2(10));Your scenario is not very clear to me, but let us do some test coding here, see if it helps you,SQL> create or replace package test_pkg1 as
2 TYPE cursor_type1 IS REF CURSOR;
3 procedure proc1;
4 end;
5 /
Package created.
SQL> create or replace package test_pkg2 as
2 TYPE cursor_type2 IS REF CURSOR;
3 procedure proc2(pCur IN OUT cursor_type2);
4 end;
5 /
Package created.
SQL> create or replace package body test_pkg2 as
2 procedure proc2(pCur IN OUT cursor_type2) is
3 begin
4 open pCur for SELECT ename from my_emp;
5 end;
6 end;
7 /
Package body created.
SQL> create or replace package body test_pkg1 as
2 procedure proc1 is
3 vRefCur cursor_type1;
4 vEname VARCHAR2(20);
5 vRefCur2 test_pkg2.cursor_type2;
6 begin
7 test_pkg2.proc2(vRefCur); -- this is possible, but I do not like it.
8 vRefCur2 := vRefCur; -- you can also do this, but you have no reason to do this
9 loop
10 fetch vRefCur2 into vEname;
11 exit when vRefCur2%NOTFOUND;
12 dbms_output.put_line(vEname);
13 end loop;
14 close vRefCur2;
15 end;
16 end;
17 /
Package body created.
SQL> exec test_pkg1.proc1;
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
PL/SQL procedure successfully completed.
Let us make it simple,
SQL> create or replace package body test_pkg1 as
2 procedure proc1 is
3 vRefCur cursor_type1;
4 vEname VARCHAR2(20);
5 --vRefCur2 test_pkg2.cursor_type2;
6 begin
7 test_pkg2.proc2(vRefCur);
8 --vRefCur2 := vRefCur;
9 loop
10 fetch vRefCur into vEname;
11 exit when vRefCur%NOTFOUND;
12 dbms_output.put_line(vEname);
13 end loop;
14 close vRefCur;
15 end;
16 end;
17 /
Package body created.
SQL> exec test_pkg1.proc1;
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
PL/SQL procedure successfully completed.You can play with it number of different ways.
Let me know what questions and we will take it from there.
Thx,
SriDHAR -
Dynamic sql and ref cursors URGENT!!
Hi,
I'm using a long to build a dynamic sql statement. This is limited by about 32k. This is too short for my statement.
The query results in a ref cursor.
Does anyone have an idea to create larger statement or to couple ref cursors, so I can execute the statement a couple of times and as an result I still have one ref cursor.
Example:
/* Determine if project is main project, then select all subprojects */
for i in isMainProject loop
if i.belongstoprojectno is null then
for i in ProjectSubNumbers loop
if ProjectSubNumbers%rowcount=1 then
SqlStatement := InitialStatement || i.projectno;
else
SqlStatement := SqlStatement || PartialStatement || i.projectno;
end if;
end loop;
else
for i in ProjectNumber loop
if ProjectNumber%rowcount=1 then
SqlStatement := InitialStatement || i.projectno;
else
SqlStatement := SqlStatement || PartialStatement || i.projectno;
end if;
end loop;
end if;
end loop;
/* Open ref cursor */
open sql_output for SqlStatement;
Thanks in advance,
Jeroen Muis
KCI Datasystems BV
mailto:[email protected]Example for 'dynamic' ref cursor - dynamic WHERE
(note that Reports need 'static' ref cursor type
for building Report Layout):
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
ref_cur_dyn type_ref_cur_dyn;
BEGIN
OPEN ref_cur_dyn FOR
'SELECT * FROM dept WHERE ' | | NVL (p_where, '1 = 1');
RETURN ref_cur_dyn;
END;
END;
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;
Regards
Zlatko Sirotic
null -
ORA-01008 with ref cursor and dynamic sql
When I run the follwing procedure:
variable x refcursor
set autoprint on
begin
Crosstab.pivot(p_max_cols => 4,
p_query => 'select job, count(*) cnt, deptno, row_number() over (partition by job order by deptno) rn from scott.emp group by job, deptno',
p_anchor => Crosstab.array('JOB'),
p_pivot => Crosstab.array('DEPTNO', 'CNT'),
p_cursor => :x );
end;I get the following error:
^----------------
Statement Ignored
set autoprint on
begin
adsmgr.Crosstab.pivot(p_max_cols => 4,
p_query => 'select job, count(*) cnt, deptno, row_number() over (partition by
p_anchor => adsmgr.Crosstab.array('JOB'),
p_pivot => adsmgr.Crosstab.array('DEPTNO', 'CNT'),
p_cursor => :x );
end;
ORA-01008: not all variables bound
I am running this on a stored procedure as follows:
create or replace package Crosstab
as
type refcursor is ref cursor;
type array is table of varchar2(30);
procedure pivot( p_max_cols in number default null,
p_max_cols_query in varchar2 default null,
p_query in varchar2,
p_anchor in array,
p_pivot in array,
p_cursor in out refcursor );
end;
create or replace package body Crosstab
as
procedure pivot( p_max_cols in number default null,
p_max_cols_query in varchar2 default null,
p_query in varchar2,
p_anchor in array,
p_pivot in array,
p_cursor in out refcursor )
as
l_max_cols number;
l_query long;
l_cnames array;
begin
-- figure out the number of columns we must support
-- we either KNOW this or we have a query that can tell us
if ( p_max_cols is not null )
then
l_max_cols := p_max_cols;
elsif ( p_max_cols_query is not null )
then
execute immediate p_max_cols_query into l_max_cols;
else
RAISE_APPLICATION_ERROR(-20001, 'Cannot figure out max cols');
end if;
-- Now, construct the query that can answer the question for us...
-- start with the C1, C2, ... CX columns:
l_query := 'select ';
for i in 1 .. p_anchor.count
loop
l_query := l_query || p_anchor(i) || ',';
end loop;
-- Now add in the C{x+1}... CN columns to be pivoted:
-- the format is "max(decode(rn,1,C{X+1},null)) cx+1_1"
for i in 1 .. l_max_cols
loop
for j in 1 .. p_pivot.count
loop
l_query := l_query ||
'max(decode(rn,'||i||','||
p_pivot(j)||',null)) ' ||
p_pivot(j) || '_' || i || ',';
end loop;
end loop;
-- Now just add in the original query
l_query := rtrim(l_query,',')||' from ( '||p_query||') group by ';
-- and then the group by columns...
for i in 1 .. p_anchor.count
loop
l_query := l_query || p_anchor(i) || ',';
end loop;
l_query := rtrim(l_query,',');
-- and return it
execute immediate 'alter session set cursor_sharing=force';
open p_cursor for l_query;
execute immediate 'alter session set cursor_sharing=exact';
end;
end;
/I can see from the error message that it is ignoring the x declaration, I assume it is because it does not recognise the type refcursor from the procedure.
How do I get it to recognise this?
Thank you in advanceThank you for your help
This is the version of Oracle I am running, so this may have something to do with that.
Oracle9i Enterprise Edition Release 9.2.0.8.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.8.0 - Production
I found this on Ask Tom (http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:3027089372477)
Hello, Tom.
I have one bind variable in a dynamic SQL expression.
When I open cursor for this sql, it gets me to ora-01008.
Please consider:
Connected to:
Oracle8i Enterprise Edition Release 8.1.7.4.1 - Production
JServer Release 8.1.7.4.1 - Production
SQL> declare
2 type cur is ref cursor;
3 res cur;
4 begin
5 open res for
6 'select * from (select * from dual where :p = 1) connect by 1 = 1'
7 using 1;
8 end;
9 /
declare
ERROR at line 1:
ORA-01008: not all variables bound
ORA-06512: at line 5
SQL> declare
2 type cur is ref cursor;
3 res cur;
4 begin
5 open res for
6 'select * from (select * from dual where :p = 1) connect by 1 = 1'
7 using 1, 2;
8 end;
9 /
PL/SQL procedure successfully completed.
And if I run the same thing on 10g -- all goes conversely. The first part runs ok, and the second
part reports "ORA-01006: bind variable does not exist" (as it should be, I think). Remember, there
is ONE bind variable in sql, not two. Is it a bug in 8i?
What should we do to avoid this error running the same plsql program code on different Oracle
versions?
P.S. Thank you for your invaluable work on this site.
Followup June 9, 2005 - 6pm US/Eastern:
what is the purpose of this query really?
but it would appear to be a bug in 8i (since it should need but one). You will have to work that
via support. I changed the type to tarray to see if the reserved word was causing a problem.
variable v_refcursor refcursor;
set autoprint on;
begin
crosstab.pivot (p_max_cols => 4,
p_query =>
'SELECT job, COUNT (*) cnt, deptno, ' ||
' ROW_NUMBER () OVER ( ' ||
' PARTITION BY job ' ||
' ORDER BY deptno) rn ' ||
'FROM emp ' ||
'GROUP BY job, deptno',
p_anchor => crosstab.tarray ('JOB'),
p_pivot => crosstab.tarray ('DEPTNO', 'CNT'),
p_cursor => :v_refcursor);
end;
/Was going to use this package as a stored procedure in forms but I not sure it's going to work now. -
Ref cursors and dynamic sql..
I want to be able to use a fuction that will dynamically create a SQL statement and then open a cursor based on that SQL statement and return a ref to that cursor. To achieve that, I am trying to build the sql statement in a varchar2 variable and using that variable to open the ref cursor as in,
open l_stmt for refcurType;
where refcurType is a strong ref cursor. I am unable to do so because I get an error indication that I can not use strong ref cursor type. But, if I can not use a strong ref cursor, I will not be able to use it to build the report based on the ref cursor because Reports 9i requires strong ref cursors to be used. Does that mean I can not use dynamic sql with Reports 9i ref cursors? Else, how I can do that? Any documentation available?Philipp,
Thank you for your reply. My requirement is that, sometimes I need to construct a whole query based on some input, and sometimes not. But the output record set would be same and the layout would be more or less same. I thought ref cursor would be ideal. Ofcourse, I could do this without dynamic SQL by writing the SQL multiple times if needed. But, I think dynamic SQL is a proper candidate for this case. Your suggestion to use lexical variable is indeed a good alternative. In effect, if needed, I could generate an entire SQL statement and place in some place holder (like &stmt) and use it as a static SQL query in my data model. In that case, why would one ever need ref cursor in reports? Is one more efficient over the other? My guess is, in the lexical variable case, part of the processing (like parsing) is done on the app server while in a function based ref cursor, the entire process takes place in the DB server and there is probably a better chance for re-use(?)
Thanks,
Murali. -
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. ;) -
Using ref cursor in where clause
I have a stored procedure (package procedure) that returns a ref cursor as a parameter. This procedure performs a query and returns the cursor. The procedure calls another stored procedure within the package. This second procedure also returns a ref cursor. I want to do my query on the results of the cursor returned by the second procedure. Hopefully the example below is okay.. "I had to change names to protect the blah blah...."
For example:
TYPE out_cursor IS REF CURSOR;
TYPE IDTable IS TABLE OF table1.ID%TYPE;
PROCEDURE A
v_ID IN NUMBER,
v_Param2 IN NUMBER,
v_FromClause IN VARCHAR2, -- ignore for now
v_WhereClause IN VARCHAR2, -- ignore for now
v_retval OUT NUMBER,
the_cursor OUT out_cursor
IS
v_SQLString VARCHAR2(2048);
v_cursor1 out_cursor;
v_tabIDs IDTable;
BEGIN
v_SQLString := '';
packageA.procedureA(v_ID, v_retval, v_cursor1);
-- this does not work
-- I get ORA-00942: table or view does not exist
v_SQLString := 'select distinct * from v_cursor1';
-- I found this on the message board but
-- it did not compile.
-- local collection types not allowed in SQL statements
FETCH v_cTSFiles BULK COLLECT INTO v_tabIDs;
v_SQLString := 'select distinct * from table1 WHERE table1.ID in (select * from table(v_tabIDs))';
OPEN the_cursor FOR v_SQLString;
END;You can use that second technique but as the error message says, "local collection types not allowed in SQL statements". You need to create a schema type instead.
[sql]
create or replace type ttab_number is table of number
create or replace procedure p is
tab_id ttab_number;
refcur sys_refcursor;
begin
open refcur for
select user_id
from all_users;
fetch refcur bulk collect into tab_id;
close refcur;
for rec in (
select *
from all_users
where user_id in (
select *
from table(cast(tab_id as ttab_number)))
) loop
dbms_output.put_line(rec.username);
end loop;
end;
[sql] -
Ref cursors in Database adapter
Hi,
Is the database adapter capable of handling ref cursors as the datatype of the output parameters of a pl/sql procedure?
For instance if I have the following in my package-spec:
TYPE SomeRecordType IS RECORD
( record_pk mut_table.record_pk%TYPE
, person_nr person_table.person_nr%TYPE
, field_1 mut_table.field_1%type
, field_2 mut_table.field_2%type
, field_3 mut_table.field_3%type
TYPE SomeCursorType IS REF CURSOR RETURN SomeRecordType;
PROCEDURE read_records
( cursor_out OUT SomeCursorType
, exception_code OUT number
, exception_message OUT varchar2
Can the database adapter call the read_records procedure?
I've never seen this in any doc. I know it can't handle record types (that is in 10.1.2 it couldn't as far as I know). So I figure that the above is not possible.
Thanks in advance.
Regards,
MartienWe have successfully used a sys_refcursor OUT parameter for a database procedure call and which is used by a DBAdapter to return a single dataset.
At the time I remember attempting to use a strongly typed ref cursor as the parameter. I think this is what you are attempting to do. I rejected this approach at the time as I was not able to do this. It was, in our case, as simple as using the system defined ref cursor type (i.e. weakly typed).
The handling of the returned dataset was not immediately obvious, but can be handled by as fairly simple XSL transformation to a locally defined variable of the requisite xml structure. I won't describe in detail how to do it as it is specific to our process. Suffice to say the transformation loops over all result rows assign via a test to the correct result field in our local variable.
e.g.
<xsl:template match="/">
<ns1:BatchRequest004>
<xsl:for-each select="/db:OutputParameters/db:P_SCHSHP_REF_CUR/db:Row">
<ns1:statusRqst>
<xsl:if test='db:Column/@name = "ID"'>
<xsl:attribute name="id">
<xsl:value-of select="db:Column[1.0]"/>
</xsl:attribute>
</xsl:if>
</ns1:statusRqst>
</xsl:for-each>
</ns1:BatchRequest004>
HTH and that I haven't misidentified your problem. -
Hi,
If anybody know how to execute function which has ref cursor type.
Can I call that function in my select statement in query.
Function is using week ref cursor type and id as IN parameter.
Thans for you help in advance.You can use a function returning a ref cursor in SQL, the output is nested cursor though.
SQL> create or replace function f
2 return sys_refcursor
3 as
4 c sys_refcursor;
5 begin
6 open c for
7 select * from emp where deptno = 10;
8 return c;
9 end;
10 /
Function created.
SQL> select f from dual;
F
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7782 CLARK MANAGER 7839 06-09-1981 2450 10
7839 KING PRESIDENT 11-17-1981 5000 10
7934 MILLER CLERK 7782 01-23-1982 1300 10
SQL> -
Open ref cursor in a procedure
Hi
I have a procedure which has 3 input parameters and 1 REF CURSOR type OUT parameter.
In the body of the procedure a select query is dynamically being created, and just before the end of the procedure the REF CURSOR is opened for SELECT(dynamic) query
For Ex: Create or replace procedure proc_name(a in number,b in number,c in number,RETVAL OUT REF CURSOR) as
decalre
strQry varchar2(3000);
begin
-- strQry is select query built dynamically
OPEN RETVAL for strQry;
end proc_name;
This procedure is being called by a java application. My question is what the open cursor statement is doing in the procedure?
Any ideas? Thanks!
greddyHi,
As i know, for example if we want to generate a report for some particular records in our project.consider we are using java as front end and oracle as backend. ok. In that case we need to bring the data from backend to front end ,for that in backend (oracle) we can write procedure to perform this task using ref cursors.
Note: ref cursor will provide the most efficient way in bringing the records from back end to front end.
Create or replace procedure proc_name(a in number,b in number,c in number,RETVAL OUT REF CURSOR) as
decalre
strQry varchar2(3000);
begin
-- strQry is select query built dynamically
--if we are using dynamic query, we can filter out the records using id or name or date
once we did, we need to pass the values to calling enviroment, since we declared the ref cursor as out parameter, we can use the ref cursor to pass the values to calling enviroment. Since we are using ref cursor we need to open the cursor for that dynamic query.
OPEN RETVAL for strQry;
end proc_name;
hope this information is some what helpful. if you got any details regarding this please post it..
thanks. -
Is there a different way to open a cursor for a ref cursor procedure?
hello everybody
i have two cursors, cur_a and cur_b, declared somewhere else in my application.
These two cursors have the same fields, in the same order, and i have to treat both in the same way. So i wrote a routine that gets as input a ref cursor based on the cur_a rowtype, and i am trying to use this routine for both.
The problem is that i am not able to open outside the routine the cursor in a different way than usual...
the common method is :
declare curs ref cursor ...
begin
open curs for (select *...)
end;
instead i would like to obtain something different
declare curs ref cursor ...
begin
open curs for cur_a
end;hi
thanks for answering
i wanted just to give a better idea, anyway you were near to get it.
the only difference is that the two cursors are not written in dynamic sql, just like strings, but are real cursors.
anyway, this is the version of the package i need, but i am not able to compile
(your original code is commented and immediately below there is my code)
CREATE OR REPLACE PACKAGE BODY mytest
IS
--cur_a VARCHAR2(200) := 'SELECT dummy FROM DUAL';
CURSOR cur_a
IS
SELECT dummy
FROM DUAL;
--cur_b VARCHAR2(200) := 'SELECT ''fred'' FROM DUAL';
CURSOR cur_b
IS
SELECT 'fred' fred
FROM DUAL;
TYPE t_cur_a IS REF CURSOR
RETURN cur_a%ROWTYPE
--PROCEDURE routine_a_b (p_cur SYS_REFCURSOR) IS
PROCEDURE routine_a_b (p_cur t_cur_a)
IS
v_x VARCHAR2 (10);
BEGIN
LOOP
FETCH p_cur
INTO v_x;
EXIT WHEN p_cur%NOTFOUND;
DBMS_OUTPUT.put_line (v_x);
END LOOP;
END;
PROCEDURE doit
IS
--v_curs SYS_REFCURSOR;
v_curs t_cur_a;
BEGIN
NULL;
-- open v_curs FOR cur_a;
OPEN v_curs FOR cur_a;
routine_a_b (v_curs);
CLOSE v_curs;
-- open v_curs FOR cur_b;
-- routine_a_b(v_curs);
-- close v_curs;
END;
END;
the error is:
cursor 'V_CURS' cannot be used in dynamic SQL OPEN statement
i did read that if use weak ref cursor, it could work, so i declare the ref cursor type in this way:
TYPE t_cur_a IS REF CURSOR;
instead than
TYPE t_cur_a IS REF CURSOR
RETURN cur_a%ROWTYPE
what i get is another error (in the open cursor command)
PLS-00382: expression is of wrong type....
but if i replace
OPEN v_curs FOR cur_a;
with
OPEN v_curs for select dummy from dual;
it works... but i already knew it.. :-)
anyway, i used a work around to resolve it, so it's just philosophy -
How to out Dynamic ref cursor from Procedure to Forms
Hi
I am trying to out Dynamic ref cursor from Procedure to Forms, But I am unable to do so. however cursor return the value within procedure but I am failed to capture the same in Forms
Pl advice suggestion if any, Here I am attaching full procedure for reference
CREATE PACKAGE winepkg
IS
TYPE wine IS RECORD ( mynumber number);
/* Define the REF CURSOR type. */
TYPE wine_type IS REF CURSOR RETURN wine;
END winepkg;
CREATE procedure find_wine
(col1_in in number,
c1 out winepkg.wine_type) as
vsql varchar2(1000);
cur sys_refcursor;
x number;
BEGIN
vsql:='select bo_id from bo_details where bo_details.bo_id = '||col1_in ;
open cur for vsql;
c1:=cur;
--fetch c1 into x;
--dbms_output.put_line(x);
END find_wine;
In front end forms
Declare
TYPE F is REF CURSOR;
CUR_F F;
rec number;
Begin
break;
find_wine( 1601480000011078,cur_f ) ;
Loop
fetch cur_f into rec ;
Message(rec ) ;pause;
exit when cur_f%notfound ;
End loop ;
exception
when others then
Message(sqlerrm) ;pause;
End ;yo can use
declare
c_cursor EXEC_SQL.CursType;
v_stmt varchar2(2000) = 'select a, b, c from mytab where cond1'; -- you can create this value dynamically
begin
c_cursor := Exec_SQL.Open_cursor;
EXEC_SQL.PARSE(c_articulos, v_stmt);
EXEC_SQL.DEFINE_COLUMN(c_articulos,1, v_colchar1, 30);
EXEC_SQL.DEFINE_COLUMN(c_articulos,2, v_colchar2, 15);
EXEC_SQL.DEFINE_COLUMN(c_articulos,3, v_colchar3, 30);
v_exec := EXEC_SQL.EXECUTE(c_cursor);
WHILE EXEC_SQL.FETCH_ROWS(c_cursor) > 0 LOOP
EXEC_SQL.COLUMN_VALUE(c_cursor,1,v_colchar1);
EXEC_SQL.COLUMN_VALUE(c_c_cursor,2,v_colchar2);
EXEC_SQL.COLUMN_VALUE(c_c_cursor,3,v_colchar3);
assign_values_to_block;
END LOOP;
EXEC_SQL.CLOSE_CURSOR(c_cursor);
end;
and WORKS IN FORMS 6 -
Problem declaring and using a REF CURSOR
I'm having a real problem using a REF CURSOR type
Here's the DECLARE and the start of the BEGIN I've so far developed.
DECLARE
TYPE r1 IS RECORD (
szvcapc_pidm szvcapc.szvcapc_pidm%TYPE,
szvcapc_term_code szvcapc.szvcapc_term_code%TYPE,
szvcapc_request_no szvcapc.szvcapc_request_no%TYPE);
szvcapc_rec r1;
TYPE cursor_1 IS REF CURSOR RETURN r1;
szvcapc_cv cursor_1;
TYPE r2 IS RECORD (
stvests_code stvests.stvests_code%TYPE
stvests_rec r2;
TYPE cursor_2 IS REF CURSOR RETURN stvests_rec;
stvests_cv cursor_2;
BEGIN
OPEN szvcapc_cv FOR
SELECT szvcapc_pidm, szvcapc_term_code, szvcapc_request_no
FROM szvcapc
WHERE szvcapc_passed_ind = 'Y'
AND szvcapc_award_credits = 'N';
LOOP
FETCH szvcapc_cv INTO szvcapc_rec;
EXIT WHEN szvcapc_cv%NOTFOUND;
END LOOP;
OPEN stvests_cv FOR
SELECT stvests_code
FROM stvests
WHERE stvests_eff_headcount = 'Y';
LOOP
FETCH stvests_cv INTO stvests_rec;
EXIT WHEN stvests_cv%NOTFOUND;
END LOOP;
SELECT *
FROM (
<snip>
INNER JOIN stvests_rec
ON SFBETRM.SFBETRM_ESTS_CODE = stvests_rec.STVESTS_CODE
<snip>
I later try to use the stvests_rec and szvcapc_rec in the main SELECT statement it doesn't recognise stvests_rec and szvcapc_rec as a "Table or View".
I have to use a REF CURSOR as this code is ultimately for use in Oracle Reports.
What am I doing wrong?> The reason I'm trying to use a REF CURSOR is that I was told that you
couldn't use CURSORs in Oracle Reports.
That does not change anything ito what happens on the Oracle server side. A cursor is a cursor is a cursor.
Every single SQL winds up as a cursor. Each cursor has a reference handle to access and use. HOW this handle is used in the language differs. But that is a language issue and not an Oracle RDBMS issue.
For example. An EXECUTE IMMEDIATE in PL/SQL creates a cursor handle and destroys it after use - automatically. An implicit cursor works the same. An explicit cursor you have the handle fetch from it and close from it when done.
A ref cursor is simply a handle that can be returned to an external client - allowing that application to use the handle to fetch the rows.
Do not think that a ref cursor is any different from the RDBMS side than any other cursor. The RDBMS does not know the difference. Does not care and nor should it.
> I'm trying to reduce the hits on the database from nested selects by
removing the dataset from the main SELECT statement and performing it only
once outside and then referencing this previously collected dataset inside the
main SELECT statement.
Good stuff that you are considering SQL performance. But you need to make sure that you
a) identify the performance inhibitor issue correctly
b) address this issue correctly
And you need to do that within SQL. Not PL/SQL. PL/SQL will always be slower at crunching data than SQL. For example, wanting to cache the data somehow to reduce the read overhead - that is exactly what the DB buffer cache does. It caches data. That is also exactly what the CBO will attempt - reduce the amount of data that needs to be read and processed.
Not saying that the RDBMS can do it all. It needs help from you - in the form of a SQL that instructs it to process only the minimum amount of data required to get the required results. In the form of a sound physical design that provides optimal usage of data storage and access (like indexing, partitioning, clustering, etc).
Bottom line - you cannot use a REF CURSOR to make a SQL go faster. A REF CURSOR is simply a cursor in the SQL Engine. A cursor is nothing but the "compiled-and-executable" code of a SQL "source program".
Maybe you are looking for
-
AIR 3.9 crashes on Mac OS X 10.9.1
One of our users has suddenly started experiencing crashes on Mac. There has been no change to the application. Has already tried re-installing the app as well as AIR 3.9. What could be wrong? Any ideas? Thanks. Process: Convo [1655] Path:
-
How do you move them to the Phone memory? I have pics and vids I'd like to save from my old phone and can't , for the life of me, figure out how to movw them off of the SD card and onto the phone. Either all at once or individually. Then I can move t
-
Hi, Below is my requirement. Any ideas how to make the program wait? I have Z program has 4 different process chains that needs to be triggered. When the user executes the program, I trigger an event using FM. A process chain in BW will get executed
-
Pass login credentials into a test-path cmdlet
Here is the scenario. I'm creating a SQL Server Job that will use Powershell to test if a session could be made to a remote server before creating one. I was able to successfully create a cmdlet that will test a path that connects to a remote shared
-
Here at our school, we have our students submit images in a particular folder structure. An assignment folder with 2 child folders (all and best). Is there a way (short of exp as cat) for them to organize in LR and get LR to maintain the parent ch