REF CURSOR in PL/SQL BLOCK
I am trying to return a ref cursor from a function in a package. I am trying to execute this through SQL Plus and cannot seem to get the code correct this is what I have. Please help. How do I display the output? I tried a CURSOR FOR loop, but got syntactical error saying it was being used inproperly.
DECLARE
v_rec adp_vsp.grc_vsp;
v_fname ps_personal_data.first_name%TYPE;
v_lname ps_personal_data.last_name%TYPE;
BEGIN
v_fname := 'John';
v_lname := 'Barr';
v_rec := adp_vsp.f_get_vsp(v_fname,v_lname);
END;
If you want to do it manually, you need to loop through the cursor and fetch the results:
sql>create or replace procedure p_show_emps(p_rc out sys_refcursor)
2 is
3 begin
4 open p_rc for select empno, ename from emp where rownum <= 5;
5 end;
6 /
Procedure created.
sql>declare
2 v_rc sys_refcursor;
3 v_empno emp.empno%type;
4 v_ename emp.ename%type;
5 begin
6 p_show_emps(v_rc);
7 loop
8 fetch v_rc into v_empno, v_ename;
9 exit when v_rc%notfound;
10 dbms_output.put_line( v_empno || ', ' || v_ename );
11 end loop;
12 close v_rc;
13 end;
14 /
7369, SMITH
7499, ALLEN
7521, WARD
7566, JONES
7654, MARTIN
PL/SQL procedure successfully completed.
Similar Messages
-
How to print/store in file the ref cursor in pl/sql block ?
How to print/store in file the ref cursor in pl/sql block ?.
How to print/store in file the ref cursor in pl/sql block ?.You question is quite confusing?
So, i'm providing link in this manner.
For RefCursor,
http://www.oracle-base.com/articles/misc/UsingRefCursorsToReturnRecordsets.php
http://www.oracle.com/technology/oramag/code/tips2003/042003.html
For UTL_FILE,
http://www.morganslibrary.org/reference/utl_file.html
Regards.
Satyaki De.
Updated with new morgan library link.
Edited by: Satyaki_De on Feb 24, 2010 9:03 PM -
Report using ref cursor or dynamic Sql
Hi,
I never create a report using a ref cursor or a dynamic sql. Could any one help me to solve the below issue.
I have 2 tables.
1. Student_Record
2. Student_csv_help
Student_Record the main table where the data is stored.
Student_csv_help will contain the all the column names of the Student_record.
CREATE TABLE Student_CSV_HELP
ENTRY_ID NUMBER,
RAW_NAME VARCHAR2(40 BYTE),
DESC_NAME VARCHAR2(1000 BYTE),
IN_OUTPUT_LIST VARCHAR2(1 BYTE)
SET DEFINE OFF;
Insert into TOA_CSV_HELP
(ENTRY_ID, RAW_NAME, DESC_NAME, IN_OUTPUT_LIST)
Values
(1, 'S_ID', 'Student ID', 'Y');
Insert into TOA_CSV_HELP
(ENTRY_ID, RAW_NAME, DESC_NAME, IN_OUTPUT_LIST)
Values
(2, 'S_Name', 'Student Name', 'Y');
Insert into TOA_CSV_HELP
(ENTRY_ID, RAW_NAME, DESC_NAME, IN_OUTPUT_LIST)
Values
(3, 'S_Join_date', 'Joining Date', 'Y');
Insert into TOA_CSV_HELP
(ENTRY_ID, RAW_NAME, DESC_NAME, IN_OUTPUT_LIST)
Values
(4, 'S_Address', 'Address', 'Y');
Insert into TOA_CSV_HELP
(ENTRY_ID, RAW_NAME, DESC_NAME, IN_OUTPUT_LIST)
Values
(5, 'S_Fee', 'Tution Fee', 'N');
commit;
CREATE TABLE Student_record
S_ID NUMBER,
S_Name VARCHAR2(100 BYTE),
S_Join_date date,
S_Address VARCHAR2(360 BYTE),
S_Fee Number
Insert into Student_record
(S_ID, S_Name, S_Join_date, S_Address,S_Fee)
Values
(101, 'john', TO_DATE('12/17/2009 08:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'CA-94777', 2000);
Insert into Student_record
(S_ID, S_Name, S_Join_date, S_Address,S_Fee)
Values
(102, 'arif', TO_DATE('12/18/2009 08:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'CA-94444', 3000);
Insert into Student_record
(S_ID, S_Name, S_Join_date, S_Address,S_Fee)
Values
(103, 'raj', TO_DATE('12/19/2009 08:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'CA-94555', 2500);
Insert into Student_record
(S_ID, S_Name, S_Join_date, S_Address,S_Fee)
Values
(104, 'singh', TO_DATE('12/20/2009 08:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'CA-94666', 2000);
Commit;
Now my requirement is:
I have a form with Student_record data block. When i Click on print Button on this form. It will open another window which has Student_CSV_HELP.DESC_NAME and a check box before this.
The window look like as below:
check_box DESC_NAME+
X S_ID+
-- S_Name+
X S_Join_date+
X S_Address+
-- S_Fee+
X means check box checked.+
-- means check box Unchecked.+
After i selected these check boxes i will send 2 parameters to the report server
1. a string parameter to the report server which has the value 'S_ID,S_Join_date,S_Address' (p_column_name := 'S_ID,S_Join_date,S_Address');
2. the s_id value from the student_record block (p_S_id := '101');
Now my requirement is when i click on run. I need a report like as below:
Student ID : 101+
Joining Date : 12/17/2009 08:00:00+
Address : CA-94777+
This is nothing but the ref cursor should run like as below:
Select S_id from student_record block S_id = :p_S_id;
Select S_Join_date from student_record block S_id = :p_S_id;
Select S_Address from student_record block S_id = :p_S_id;
So, according to my understanding i have to select the columns at the run time. I dont have much knowledge in creating reports using ref cursor or dynamic sql.
So please help me to solve this issue.
Thanks in advance.Plain sql should satisfy your need. Try ....
Select S_id, S_Join_date, S_Address
from student_record
where S_id = :p_S_id -
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. -
Hi..
I'm using a ref cursor query to fetch data for a report and works just fine. However i need to use dynamic sql in the query because the columns used in the where condition and for some calculations may change dynamically according to user input from the form that launches the report..
Ideally the query should look like this:
select
a,b,c
from table
where :x = something
and :y = something
and (abs(:x/:y........)
The user should be able to switch between :x and :y
Is there a way to embed dynamic sql in a ref cursor query in Reports 6i?
Reports 6i
Forms 6i
Windows 2000 PROHello Nicola,
You can parameterize your ref cursor by putting the query's select statement in a procedure/function (defined in your report, or in the database), and populating it based on arguments accepted by the procedure.
For example, the following procedure accepts a strongly typed ref cursor and populates it with emp table data based on the value of the 'mydept' input parameter:
Procedure emp_refcursor(emp_data IN OUT emp_rc, mydept number) as
Begin
-- Open emp_data for select all columns from emp where deptno = mydept;
Open emp_data for select * from emp where deptno = mydept;
End;
This procedure/function can then be called from the ref cursor query program unit defined in your report's data model, to return the filled ref cursor to Reports.
Thanks,
The Oracle Reports Team. -
Manipulating data from a ref cursor in the data block of a form
I am using Oracle Forms 10g. I have a ref cursor which returns columns A, B, C, D, E where B, C, D, E are values. The "Query Data Source Name" attribute in the datablock contains "CCTR_Extract_pkg.cctr_refcur". The "Column Name" attribute of item A contains "A", item B contains "B", etc. All of these columns are displayed on the form and it all works perfectly.
I have a new request, the users would like a 6th column displayed - column F - where it equals D - B.
Is there any way of doing this in the form only or do I need to change the ref cursor to accomodate the new column and then the form?
If it can be achieved in the Form (only) - then how do I reference the 2 columns?
Thanks in anticipation
Michael
Edited by: user8897365 on 24-Aug-2011 10:32Is there any way of doing this in the form only or do I need to change the ref cursor to accomodate the new column and then the form? The REF_CURSOR is the data source of your block so you will have to modify the CCTR_Extract_pkg package and cctr_refcur REF_CURSOR.
If it can be achieved in the Form (only) - then how do I reference the 2 columns?After you have modified your database package, I recommend you run the Data Block Wizard on your block and let Forms detect the 2 new columns. You can do this manually, but it is safer to let Forms do it for you. If you want to do it manually, open the Query Data Source Columns property and add your new columns.
Hope this helps,
Craig B-)
If someone's response is helpful or correct, please mark it accordingly. -
Ref Cursor from PL/SQL Table
I'm building a complex report in which the information cames from a lot of tables and needs complex formatting. I'd like to base my report in a table returned by a stored procedure (like you can do in Forms). Is there any way to convert an PL/SQL table in a Ref Cursor?
Why don't you process the data in procedure and dump into temporary table. And base your report on that temporary table.
it may simplify your report.
Atul -
Problem in working with multiple cursors in pl/sql block
I want to develop a procedure to update the values of a colunm for each table.
Output of cursor (cur_ttbl)will be like :
Table Name column_name
Tab1 Col1
Tab2 col2
tab3 col3
Now see below code : In below code I want to append the logic to create a update script to update the table/column returned by first cursor.. I know we have to create a second cursor to go through each record and update that... please suggest the code to do same.
CREATE OR REPLACE PROCEDURE SOL_TEST(p_message_out OUT VARCHAR2,p_err_ind_out OUT NUMBER)
IS
CURSOR cur_ttbl IS
SELECT table_name,column_name
FROM port_table;
lv_SQL_Txt VARCHAR2(4096);
v_COLNM VARCHAR2(200);
BEGIN
FOR rec_ttbl IN cur_ttbl LOOP
v_COLNM := rec_ttbl.column_name;
-- Upper case A replacement
IF INSTR(v_COLNM,chr(63),1) <> 0
THEN v_COLNM := TRANSLATE(v_COLNM,chr(63),chr(42));
END IF;
-- Update the Clintrial record
lv_SQL_Txt := 'UPDATE ' || cur_ttbl.table_name || ' SET ' || cur_ttbl.column_name || ' = TRIM('||v_COLNM||'), ' ' WHERE '|| 'STATUS= 0 AND STAT= ' || '''' || 'N' || '''';
EXECUTE IMMEDIATE lv_SQL_Txt;
lv_SQL_Txt := 'COMMIT';
EXECUTE IMMEDIATE lv_SQL_Txt;
END LOOP;
p_err_ind_out := 0;
p_message_out := '''';
EXCEPTION
WHEN NO_DATA_FOUND THEN NULL;
WHEN OTHERS THEN
p_err_ind_out := 1;
p_message_out := SUBSTR(SQLERRM,1,132);
END;
/Devgan wrote:
I want to develop a procedure to update the values of a colunm for each table.To be brutally honest - I see everything wrong with this sample procedure and nothing correct.
It does not use exceptions. Exceptions are the de facto standard in modern language - not return codes and messages.
It does not use bind variables for the dynamic SQL created. That will result in a lot of hard parsing (that will reduce that loop's performance by 500% or more). It will also fragment the Shared Pool. A fragmented Shared Pool will cause memory allocation errors.
The data model, judging from the select and update, uses column and table names as attributes. That is a fundamental violation of data modeling and makes absolutely no sense at all in relational design.
Still, I suspect that you will ignore these issues (as it seemed was done and lead to this code) and persist in doing it this "+special+" way. In which case, try not to run too hard into the brick wall. And no, one cannot hack holes in that wall. It can only be removed by adhering to fundamentals - from relational design and physical database implementation, to design and coding in PL/SQL and SQL. -
Hi all,
I have a pkg as below and its err message in compile:
create or replace PACKAGE PKG_DARTS1 is
TYPE TY_PARTY_DETAIL1 IS RECORD (
PTY_TYPE xxx.yyyy%TYPE
TYPE cur_partydetail1 IS REF CURSOR RETURN TY_PARTY_DETAIL1;
procedure SP_GetPartyDetail1( c_partydetail1 OUT pkg_darts1.cur_partydetail1) ;
end PKG_DARTS1;
create or replace PACKAGE BODY PKG_DARTS1 as
procedure SP_GetPartyDetail1( c_partydetail1 OUT pkg_darts1.cur_partydetail1)
AS
sql_stmt VARCHAR2(2000);
BEGIN
sql_stmt := 'select ''XXX'' FROM DUAL ';
OPEN c_partydetail1 FOR sql_stmt;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(sqlerrm);
END SP_GetPartyDetail1;
END PKG_DARTS1;
.Warning: Package Body created with compilation errors.
Errors for PACKAGE BODY PKG_DARTS1:
LINE/COL ERROR
13/3 PL/SQL: Statement ignored
13/8 PLS-00455: cursor 'C_PARTYDETAIL1' cannot be used in dynamic SQL
OPEN statement
what's wrong with my syntax? anything I should care in constructing sql in ref cursor?
thx,
beanYou can't use strongly types ref cursors with dynamic SQL, you will have to use weakly typed.
Hth
Martin -
Wanted to fetch data from ref cursor to nested pl/sql table getting an erro
create or replace type "DEPT12" as object(dno number(2),dname varchar2(30),loc varchar2(50));
create or replace type dept_tab as table of "DEPT12"
create or replace type "LOC12" as object(locno number,loc_name varchar2(100))
create or replace type loc_tab as table of "LOC12"
create or replace type dept_loc_rec1 as object (dept_dt dept_tab,eno number,loc_dt loc_tab);
create type dept_loc_tb as table of dept_loc_rec1
create table dept_loc_tb_bk1(dept_dt dept_tab,eno number,loc_dt loc_tab)
NESTED TABLE dept_dt
STORE AS dept_tab12,
NESTED TABLE loc_dt
STORE AS loc_tab12
insert into dept_loc_tb_bk1 values(dept_tab(dept12(3,'ABD','LOC')
,dept12(4,'ABD','LOC')
,dept12(5,'ABD','LOC')),3,loc_tab(loc12(21,'AAB'),
loc12(22,'AAB'),
loc12(23,'AAB')));
when I am trying to fetch data from ref cursor to pl/sql table which i am getting an error ora-06504: pl/sql : Return types of result set variables or query do not match.
I have created a nested table of same as the nested pl/sql object table dept_loc_tb and i have declared the lv_dept_loc_tb of same dept_loc_tb but getting an above error when trying to fetch into that variable.
Please any one who can solve my problem.
declare
type cr is ref cursor;
cr_obj cr;
lv_dept_loc_tb dept_loc_tb;
begin
open cr_obj for select dept_dt,eno,loc_dt from dept_loc_tb_bk1;
fetch cr_obj bulk collect into lv_dept_loc_tb;
close cr_obj;
end;Your query selects 3 separate columns therefore requires 3 collections of corresponding types. You want to treat those 3 columns as an object of DEPT_LOC_REC1 type:
SQL> declare
2 type cr is ref cursor;
3 cr_obj cr;
4
5 lv_dept_loc_tb dept_loc_tb;
6
7 begin
8 open cr_obj for select dept_dt,eno,loc_dt from dept_loc_tb_bk1;
9 fetch cr_obj bulk collect into lv_dept_loc_tb;
10 close cr_obj;
11 end;
12 /
declare
ERROR at line 1:
ORA-06504: PL/SQL: Return types of Result Set variables or query do not match
ORA-06512: at line 9
SQL> declare
2 type cr is ref cursor;
3 cr_obj cr;
4
5 lv_dept_loc_tb dept_loc_tb;
6
7 begin
8 open cr_obj for select DEPT_LOC_REC1(dept_dt,eno,loc_dt) from dept_loc_tb_bk1;
9 fetch cr_obj bulk collect into lv_dept_loc_tb;
10 close cr_obj;
11 end;
12 /
PL/SQL procedure successfully completed.
SQL> SY.
P.S. Discover sys_refcursor. -
Odd error while opening a ref cursor
Hi.
I have a procedure in a package that has both in and out parameters. One of those out parameters is a ref cursor. The procedure creates a dynamic query and then executes it, then it opens the cursor:
PROCEDURE PROC(
A IN VARCHAR2,
B IN VARCHAR2,
C OUT TYPES.cursorType; --(TYPES is a package whose only use is to declare a cursor type)
) IS
--DECLARATIONS
OPEN C FOR 'SELECT A, B, C, D...';
END;
When I execute the package in an anonymous block it throws the error:
ORA-00938: not enough arguments for function, just in the line where the cursor is being opened.
Any ideas?is everything defined correctly?
create or replace package types as
type cursorType is ref cursor;
end types;
SQL> set serveroutput on
SQL> declare
2
3 ref_C types.cursorType;
4
5 v_a varchar2(1);
6 v_b varchar2(1);
7 v_c varchar2(1);
8 v_d varchar2(1);
9
10 procedure Proc (a in varchar2
11 ,b in varchar2
12 ,C out types.cursorType) as
13
14 begin
15 open C for 'select :1, :2, ''c'', ''d'' from dual' using a, b;
16 end Proc;
17 begin
18
19
20 Proc('a', 'b', ref_C);
21
22 fetch ref_C into v_a, v_b, v_c, v_d;
23 if (ref_C%found) then
24 dbms_output.put_line(v_a);
25 dbms_output.put_line(v_b);
26 dbms_output.put_line(v_c);
27 dbms_output.put_line(v_d);
28 end if;
29
30
31 end;
32 /
a
b
c
dP;
Edited by: bluefrog on Feb 18, 2010 6:07 PM -
Is it possible to bind a ref cursor into dynamic SQL?
I'm trying to execute
OPEN p_recordset FOR str using parent_set;in a procedure where str =
SELECT ct.EVENTDATE, ct.EVENTID, cr.SURNAME, cr.NAME, cr.ADDRESS, cr.CITY, cr.STATE,
cr.ZIPCODE, cr.YLOC, cr.XLOC, ct.COMPTNUM, cr.COMPRNUM
FROM COMPLAINTS ct, COMPLAINERS cr,
table(f_refCurNum2(:parent_set)) e
WHERE ct.EVENTID = e.column_value
AND ct.COMPRNUM = cr.COMPRNUMrunning this query in an anonymous PL/SQL block, without binding the cursor, works fine, but with binding in the procedure I get:
ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at "PROFILER.F_REFCURNUM", line 9
ORA-06512: at line 17
Oh, and the cursor is defined in the same manner for both the procedure and the anon. PL/SQL
Any thoughts/suggestions?Found the problem. The parent_set cursor is being defined in the procedure, and needs to be passed out to the calling procedure, so that it can see this embedded cursor when the fetch is being done.
-
Problem in opening a ref cursor.
Hi,
I'm getting the following error when i'm trying to open the ref cursor. PL/SQL: ORA-22905: cannot access rows from a non-nested table item
What i'm trying to do is I'm dumping the data into pl/sql table and i want retrieving the by using a ref cursor. Please see the code and help me out.
CREATE OR REPLACE PACKAGE CPS_RECR.pg_pool_status AS
TYPE pool_rec IS RECORD (
status varchar2(50)
,stsno number
,stscode varchar2(5)
,candidatename varchar2(200)
,monyear varchar2(10)
,yyyymm number
,stscnt number
--type rec_sts_tab is table of number ;--index by pls_integer;
type pool_tab IS table of pool_rec index by binary_integer;
type pool_cv is REF CURSOR return pool_rec;
FUNCTION pool_status_query(p_start_date in date,p_end_date in date,p_invitedtopoolbit in number,p_showedForPoolBit in number)
RETURN pool_tab;--pool_cv ;
cursor cur_pool(p_start_date date,p_end_date date,p_invitedtopoolbit number,p_showedForPoolBit number)
is
SELECT distinct to_char(date1,'yyyymm')yearmonth
FROM acs100data a,
acs100_candidate_verification b,
acs100_candidate_pool d
WHERE UPPER (a.basic_email) = UPPER (b.email)
AND (b.candidate_status IN ('FORL', 'FWRT') or BITAND (b.candidate_status_bit, 4 ) > 0
or BITAND (b.candidate_status_bit, 2) > 0)
AND d.pool_id = b.pool_id
AND pool_date BETWEEN p_start_date AND p_end_date
AND is_tentative_date IS NULL ;
cursor cur_name(p_yyyymm varchar2,p_cond number,p_start_date date,p_end_date date)
is
select *
from (select distinct (case
when p_cond = 0 and b.candidate_status = 'FORL'
then (last_name || first_name)
when p_cond = 1 and b.candidate_status = 'FWRT'
then (last_name || first_name)
when p_cond = 2
and bitand (b.candidate_status_bit, p_cond) > 0
then (last_name || first_name)
when p_cond = 4
and bitand (b.candidate_status_bit, p_cond) > 0
then (last_name || first_name)
end
) candidatename
from acs100data a,
acs100_candidate_verification b,
acs100_candidate_pool d
where upper (a.basic_email) = upper (b.email)
and d.pool_id = b.pool_id
and pool_date between p_start_date and p_end_date
and to_char (date1, 'yyyymm') = p_yyyymm
and is_tentative_date is null)
where candidatename is not null;
END pg_pool_status;
CREATE OR REPLACE PACKAGE BODY CPS_RECR.pg_pool_status
AS
FUNCTION pool_status_query (
p_start_date IN DATE,
p_end_date IN DATE,
p_invitedtopoolbit IN NUMBER,
p_showedforpoolbit IN NUMBER
RETURN pool_tab--pool_cv
IS
tab_pool pool_tab;
temp_rf pool_cv;
n_index NUMBER := 1;
--rec_sts_data rec_sts_tab;
n_stscnt NUMBER;
vc_status VARCHAR2 (100);
vc_label varchar2(1000);
vc_name varchar2(100);
BEGIN
tab_pool.DELETE;
vc_label :='Opening Pool cursor';
FOR rec_pool IN cur_pool (p_start_date,
p_end_date,
p_invitedtopoolbit,
p_showedforpoolbit
LOOP
if cur_pool%notfound then
exit;
end if;
vc_label :='Opening p_cond cursor';
FOR p_cond IN 0 .. 3
LOOP
n_stscnt := 0;
vc_status := NULL;
begin
SELECT SUM
(NVL
(COUNT
(CASE
WHEN p_cond = 0
AND b.candidate_status = 'FORL'
THEN (last_name || first_name)
WHEN p_cond = 1
AND b.candidate_status = 'FWRT'
THEN (last_name || first_name)
WHEN p_cond = 2
AND BITAND (b.candidate_status_bit,
p_cond) > 0
THEN (last_name || first_name)
WHEN p_cond = 4
AND BITAND (b.candidate_status_bit,
p_cond) > 0
THEN (last_name || first_name)
END
0
) cnt,
DECODE (p_cond,
0, 'FAILED WRITTEN TEST',
1, 'FAILED **** TEST',
2, 'Invited for Pool',
4, 'Showed up for Pool'
) status
INTO n_stscnt,
vc_status
FROM acs100data a,
acs100_candidate_verification b,
acs100_candidate_pool d
WHERE UPPER (a.basic_email) = UPPER (b.email)
AND d.pool_id = b.pool_id
AND pool_date BETWEEN p_start_date AND p_end_date
AND TO_CHAR (date1, 'yyyymm') = rec_pool.yearmonth
AND is_tentative_date IS NULL
GROUP BY candidate_status,
b.candidate_status_bit,
DECODE (p_cond,
0, 'FAILED WRITTEN TEST',
1, 'FAILED **** TEST',
2, 'Invited for Pool',
4, 'Showed up for Pool'
exception
when no_data_found
then
n_stscnt :=0;
vc_status :=null;
end;
vc_label :='Opening name cursor';
FOR rec_name IN cur_name (rec_pool.yearmonth,
p_cond,
p_start_date,
p_end_date
LOOP
if cur_name%notfound then
exit;
end if;
tab_pool (n_index).yyyymm := rec_pool.yearmonth;
tab_pool (n_index).stscnt := n_stscnt;
tab_pool (n_index).status := vc_status;
tab_pool (n_index).candidatename := rec_name.candidatename;
dbms_output.put_line('tab_pool(n_index).yyyymm : '||tab_pool(n_index).yyyymm);
dbms_output.put_line('tab_pool(n_index).stscnt : '||tab_pool(n_index).stscnt);
dbms_output.put_line('tab_pool(n_index).status : '||tab_pool(n_index).status);
dbms_output.put_line('tab_pool(n_index).candidatename : '||tab_pool(n_index).candidatename);
vc_name :=rec_name.candidatename;
END LOOP;
n_index := n_index + 1;
END LOOP;
END LOOP;
RETURN tab_pool;
exception
when others
then
dbms_output.put_line('error :'||vc_label||'--'|| vc_name);
END;
END pg_pool_status;
---run script
DECLARE
RetVal CPS_RECR.PG_POOL_STATUS.pool_tab;
P_START_DATE DATE;
P_END_DATE DATE;
P_INVITEDTOPOOLBIT NUMBER;
P_SHOWEDFORPOOLBIT NUMBER;
temp_cv CPS_RECR.PG_POOL_STATUS.pool_cv;
BEGIN
P_START_DATE := to_date('09/01/2008','mm/dd/yyyy');
P_END_DATE := to_date('09/30/2008','mm/dd/yyyy');
P_INVITEDTOPOOLBIT := 2;
P_SHOWEDFORPOOLBIT := 4;
open temp_cv for select * from table((CPS_RECR.PG_POOL_STATUS.POOL_STATUS_QUERY ( P_START_DATE, P_END_DATE, P_INVITEDTOPOOLBIT, P_SHOWEDFORPOOLBIT )) );
end loop;
exception
when others
then
dbms_output.put_line(sqlerrm);
END;Satyaki,
It doesn't help me out. I'm worndering one of code sample is working fine. i didn't my current is giving the problem.
FYI, please see the some code i followed.
SQL> Create or replace PACKAGE cv IS
2 type comp_rec is RECORD
3 (deptno number,
4 ename varchar(10),
5 compensation number);
6 type comp_tbl IS table of comp_rec;
7 function get_coll return comp_tbl pipelined;
8 temp_tbl comp_tbl := comp_tbl();
9 type comp_cv is REF CURSOR return comp_rec;
10 end;
11 /
Package created.
SQL> Create or replace PACKAGE body cv IS
2 function get_coll return comp_tbl pipelined
3 is
4 begin
5 for i in 1..temp_tbl.count loop
6 pipe row(temp_tbl(i));
7 end loop;
8 return;
9 end;
10 end;
11 /
Package body created.
SQL> declare
2 temp_cv cv.comp_cv;
3 rc cv.comp_rec;
4 begin
5 cv.temp_tbl.delete;
6 cv.temp_tbl.extend;
7 cv.temp_tbl(1).deptno:=10;
8 cv.temp_tbl(1).ename:='1223';
9 cv.temp_tbl(1).compensation:=10;
10
11 -- erroring out
12 open temp_cv for select * from table(cv.get_coll);
13 fetch temp_cv into rc;
14 dbms_output.put_line('Deptno is ' || rc.deptno);
15 dbms_output.put_line('ename is ' || rc.ename);
16 end;
17 /
Deptno is 10
ename is 1223
PL/SQL procedure successfully completed. -
Oracle pl/sql block in java
Hi,
Everybody I just want to know how we execute the pl/sql (oracle9i)block(not store Procedure)
in java that have explicit cursor and how we print or display the value in cursor..
pl/sql block declare
v_id number;
cursor cv1 IS
select request_id from requests where sys_id = 3
begin\n
open cv1;
loop
fetch cv1 into v_id;
exit when cv1%notfound;
dbms_output.put_line(v_id);
end loop; \n"
end;;
i try it like simple sql query
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
conn = DriverManager.getConnection(url, name, pass);
stmt = conn.createStatement();
rs = stmt.executeQuery(pl/sql block);
I want display the value store in cursor............in java....
kindly help me........
regard's
paritosh tomarParitosh,
You can execute anonymous PL/SQL blocks in JDBC.
I suggest searching this forum's archives for the word "anonymous".
If I remember correctly, there should be a relevant post from David Rolfe from a few years ago.
Good Luck,
Avi.
Maybe you are looking for
-
Optical Bluetooth Mouse will not Pair in Snow Leopard 10.6.1 on MBP5,5
I have this mouse, with full batteries, just now tested and working on Windows XP with another laptop, and with an older MacBook Pro running 10.5. I can see this device in the Bluetooth set-up, and when I click Continue, the window shows a connection
-
Using RAW images from Canon T3i in PSE 6
I have a new Canon T3i which is set to create both RAW CR2 files and jpegs. How can I open the RAW files in Photoshop Elements 6? I've searched the internet for two hours now and can find no answer which seems unambiguous, short of buying the latest
-
How do i convert a landscape txt file to a landscape pdf. i only get portrait when i convert
When I convert a text file that is in landscape format the converted file comes in as a portrait pdf
-
"Don't show this again" checkbox in prompt?
All, So I have a few scripts that are designed to act on the currently open document. Some of them by nature are pretty destructive to the document because they're designed to do some crazy stuff. So, when the user runs the script, I'm currently pres
-
How we will display the header data and item data seperately in lists
how we will display the header data and item data seperately in lists