6i ref cursor no_data_found exception report
I have a simple report with the cursor query a Ref on a scheme of database packed.The data model function has an exception handler for no_data_found.
The exception sets a local variable in a program unit specification.
A field with a message based on a column in support of place does not return.
I try to give a message to the user and am not having any luck.
Ideas?
Thank you.
You can create a text field with the text "no data found".
This field gets a format trigger
if then
return true;
else
return false;
end;
Tags: Oracle Development
Similar Questions
-
REP-0737: must be a function of return type 'ref cursor.
Hi all
I have create a ref cursor query in reports 10 g. But it is giving error REP-0737: must be a function of return type 'ref cursor.
Here is my code
function QR_1RefCurDS return sys_refcursor is My_Cur Sys_Refcursor; begin Open My_Cur for select * from scott.emp order by deptno; return My_Cur end;
fate of the screen.
Oracle Forms/Reports has a complete PL/SQL engine and (only) the SQL parser.
However, the engine of forms/States PL / SQL and SQL Analyzer are at a level that was in the Oracle 8.0 database.
So, in the forms/States functions/procedures and forms/States triggers, you can not use SQL commands that did not exist in the 8.0 database.
The predefined SYS_REFCURSOR type is introduced in Oracle 9i.
Use this:
PACKAGE test_rc IS
TYPE of rc_type IS REF CURSOR RETURN emp % ROWTYPE;
END;
FUNCTION RETURN QR_1RefCurDS Test_rc.rc_type IS
test_rc.rc_type RC;
BEGIN
OPEN the RC to SELECT * FROM emp;
RETURN rc;
END;
Kind regards
Zlatko
-
Report regions - use of the Ref Cursor?
APEX 4.2
See http://mikesmithers.wordpress.com/2012/02/22/getting-apex-to-play-with-ref-cursors
The above article is older than 2 years. The techniques shown in the link above still valid today if we want to create a report region in the APEX are based on a function that returns a Ref Cursor? Are there improvements in this area for the next APEX 5.0 and/or database Oracle 12 c?
Thank you
HELEN wrote:
APEX 4.2
See http://mikesmithers.wordpress.com/2012/02/22/getting-apex-to-play-with-ref-cursors
The above article is older than 2 years. The techniques shown in the link above still valid today if we want to create a report region in the APEX are based on a function that returns a Ref Cursor?
If the function returns the ref cursor is the only point of access that you have data then Yes, it probably is. If you have direct access to the underlying database objects and the function does not have something very complicated, then you are probably better off creating your own body of the function returning SQL query data source.
Everything about the dynamics of ref Cursor now I divided in 2 layers, with a function for generating the dynamic SQL and another for bind parameters open the ref cursor. APEX apps can then reuse the functions of SQL query in function body return SQL query data sources (if you take a bit of care to keep the bind variable names vaguely APEX-point-friendly).
Are there improvements in this area for the next APEX 5.0 and/or database Oracle 12 c?
See this for the technical background on why APEX is not yet support ref Cursor and this for more information of the Oracle team on the possibility of future support. Obviously, he did not he in 4.1. I suspect that data REF CURSOR region sources depended on a minimum requirement of DB of the 11.1.0.6 (where was introduced support for the conversion of the REF CURSOR in the cursors DBMS_SQL). As minimum DB for APEX 5.0 version is 11.1.0.7, it would be a possible feature for 5.0, but it is not classified as one.
-
Ref Cursor remote procedure fails
Remote database server Oracle 9iR2 1
Local database server Oracle 10 g 2 2
Remote procedure:
CREATE OR REPLACE PROCEDURE p_get_results ( i_query IN VARCHAR2 , o_refc OUT sys_refcursor ) AS BEGIN IF ( i_query IS NULL ) THEN RAISE_APPLICATION_ERROR(-20001,'The paramter i_query cannot be null') ; END IF ; OPEN o_refc FOR i_query ; EXCEPTION WHEN others THEN RAISE ; END p_get_results ; /
Local PL/SQL block
SQL> var i_query varchar2(100) SQL> var o_refc refcursor SQL> exec :i_query := 'select count(*) from oo_attrs' SQL> exec p_get_results@demo_oo2 (:i_query,:o_refc) ERROR at line 1: ORA-00604: error occurred at recursive SQL level 1 ORA-01001: invalid cursor
Check the link works and request db:
SQL> select count(*) from oo_attrs@demo_oo2; COUNT(*) ---------- 225
Am I missing something here?
Am I missing something here?
Yes - you cannot use REF CURSOR over links to db.
-
Create a REF CURSOR as an array of display/temp
Hello world
I had to upgrade a number of Crystal Reports Using REF CURSOR as part of an ERP upgrade project. The new version of the ERP application supports more use of the REF CURSOR and only supports the use of views/tables.
I don't see how I can rewrite the REF CURSOR (which are defined in the PL/SQL procedures), in the form of VIEWS / TEMP TABLES with a single call. That is to say when the user runs the report.
Can anyone suggest a method of conversion of the REF CURSOR in view/Temp Table without the two steps, the steps to populate a temporary table and then, i.e. 1 - 2 - query the table in the report. ??? -I was looking at a Pipelining as an option but again could reach with only two stages, pre filling before running the report.
Any advice would be greatly received.
Kind regards
David.
I found a solution by creating a FUNCTION with a PIPELINED return cursor, and then create a temporary table in the result set. Not really ideal was to use a Pragma AUTONOMOUS_TRANSACTION, but will work for me.
Thank you
David.
-
Irregular data loss - function from PL/SQL returning data using Ref Cursor
Database Version: 10.2.0.4.0 (node 2 CARS)
The high-level process flow is as below:
(1) insert records in a few tables & commit the same
(2) call the pl/sql function to extract files (on certain conditions with joins with other tables) of the tables which are filled in step 1.
-> It uses the ORDER BY clause to queries inline & line number 5000 records return for each call.
Sense - if inline query is supposed to return 1,00,000 records then 20 calls to the same function. This, because the application cannot contain records beyond number.
(3) the data returned by the ref cursor is then processed by application (Tibco BW) to generate the flat file.
We are facing the problem of data loss in the file and there is no fixed model. It happens once between 200-300 calls process.
Resolution: When the problem occurs, triggering the process and in almost every time re-outbreak of the process provides required data.
Guidance on what could be the reason?
* Examples of Code for the function:
CREATE OR REPLACE FUNCTION FUNC_GET_HRCH_TOTAL_DATA)
outinstrid in NUMBERS
outinstrkey in NUMBERS
rownumberstart in NUMBERS
rownumbereend in NUMBERS
err_code OUT VARCHAR2,
err_msg OUT VARCHAR2)
RETURN PACK_TYPES. HRCH_TOTAL_CURSOR
IS
REF_HRCH_TOTAL_CURSOR PACK_TYPES. HRCH_TOTAL_CURSOR;
BEGIN
OPEN FOR REF_HRCH_TOTAL_CURSOR
SELECT *.
FROM (SELECT A.HIERARCHY_KEY, B.KEY, B.VAL_KEY, A.KEY_NEW, C.ITEMID, B.VAL_TAG, B.sort_order, ROWNUM ROWNUMBER
OF AOD_HRCH_ITEM A, AOD_HRCH_ATTR B, AOD_HRCH_ITEMS C
WHERE A.outputid = B.outputid
AND A.outputid = C.outputid AND A.outputkey = B.outputkey
AND A.outputkey = C.outputkey AND A.outputid = outinstrid
AND A.outputkey = outinstrkey AND A.ITEM_SEQ = B.ITEM_SEQ
AND A.ITEM_SEQ = C.ITEM_SEQ AND A.HIERARCHY_LEVEL_ORDER = B.SORT_ORDER
ORDER BY A.HIERARCHY_LEVEL_ORDER DESC)
WHERE ROWNUMBER < rownumbereend
AND ROWNUMBER > = rownumberstart;
RETURN REF_HRCH_TOTAL_CURSOR;
EXCEPTION
WHILE OTHERS
THEN
err_code: = x_progress | ' - ' || SQLCODE;
err_msg: = SUBSTR (SQLERRM, 1, 500);
END FUNC_GET_HRCH_TOTAL_DATA;
/
Published by: meet_sanc on February 16, 2013 10:42Your SELECT statement is almost certainly incorrect
SELECT * FROM ( SELECT A.HIERARCHY_KEY, B.KEY, B.VAL_KEY, A.KEY_NEW, C.ITEMID, B.VAL_TAG, B.sort_order,ROWNUM ROWNUMBER FROM AOD_HRCH_ITEM A, AOD_HRCH_ATTR B, AOD_HRCH_ITEMS C WHERE A.outputid = B.outputid AND A.outputid = C.outputid AND A.outputkey = B.outputkey AND A.outputkey = C.outputkey AND A.outputid = outinstrid AND A.outputkey = outinstrkey AND A.ITEM_SEQ = B.ITEM_SEQ AND A.ITEM_SEQ = C.ITEM_SEQ AND A.HIERARCHY_LEVEL_ORDER = B.SORT_ORDER ORDER BY A.HIERARCHY_LEVEL_ORDER DESC) WHERE ROWNUMBER < rownumbereend AND ROWNUMBER >= rownumberstart;
Since the ORDER BY is applied after the ROWNUM is assigned in this case, your query is requested for a period of 5000 lines any arbitrariness. It would be perfectly valid for a single line to return in each of your 200 different calls or for a line to return in any of them.
You definitely want to do something in the sense of the canonical askTom wire
select * from ( select a.*, rownum rnum from ( YOUR_QUERY_GOES_HERE -- including the order by ) a where rownum <= MAX_ROWS ) where rnum >= MIN_ROWS
That said, it seems inconceivable that Tibco is unable to manage a cursor that returns more than a certain number of lines. You do a ton of work to return the data pages that are certainly not necessary. Unless you're saying that you somehow paralyzed your installation of Tibco giving him a ridiculously small amount of memory to process, something doesn't look good. A slider is just a pointer - it holds that no data - so the number of lines that you can extract a slider should have no impact on the amount of memory on the client application needs.
As others have already pointed out, your exception handler is almost certainly do more harm than good. Return the error codes and error messages as from the OUT parameters, instead of simply allowing the exception to propagate deletes a ton of useful information (such as the mistake of the stack) and makes your process much less robust.
Justin
-
Initialize the Ref Cursor to avoid ORA-01001: Invalid cursor
Hello
I write a stored procedure that returns a REF CURSOR. However, there are times when the cursor is not open if certain conditions are not met, so I wonder if there is a way to initialize the REF CURSOR so that the appellant does not receive the "ORA-01001: Invalid cursor" error when you try to work with the cursor, if it has not been opened.
Any help is greatly appreciated...
Thank you
Christinecad0227 wrote:
HelloI write a stored procedure that returns a REF CURSOR. However, there are times when the cursor is not open if certain conditions are not met, so I wonder if there is a way to initialize the REF CURSOR so that the appellant does not receive the "ORA-01001: Invalid cursor" error when you try to work with the cursor, if it has not been opened.
Any help is greatly appreciated...
Thank you
ChristineThe most appropriate way would be the caller to handle the situation. The caller must capture the exception of INVALID_CURSOR and do what is necessary.
Other suggestions like having a separate Pavilion or a model select all will lead the appellant to act to the particular situation, that slider is not being opened. What is the case with the exception of INVALID_CURSOR raised by oracle.
All the need for the appellant to do is manage the exception of INVALID_CURSOR and you should be good. And also INVALID_CURSOR is not a mistake, it's an exception that has a special meaning for her. In the case you sense it takes the condition when not together to return a cursor.
-
Adding the ability to use a ref Cursor in future versions of Apex
Currently, it has been established that the Apex does not play well with the ref Cursor and how to get around that. What is a characteristic that the Apex team seeks in future versions? Currently we use Oracle 11 g and 1 g with 4.0.2 Apex. Your answer is most appreciated.
JudJasRisUntil the APEX development team adds this feature, I'd be:
(1) write a pipeline function that will consume the ref cursor and return your data in pipeline to the calling report
(2) write a procedure that will be called before the call to your report, who built a collection of apex you run your report off the coast of...
Thank you
Tony Miller
Ruckersville, WILL -
sys_refcursor and Ref Cursor
Hello
I found this diffrence between ref and sys_refcursor slider.
If you specify return_type, then the variables of type and slider REF CURSOR of this type are strong; If not,
they are weak.
The variables SYS_REFCURSOR and slider of this type are weak.
my situation is that I must write a procedure stored so many reports entery pages. little enter pages use typed data groups and can't.
so please tel me what is the best in terms of performance and maintainability above three conditions?
Yours sincerely944768 wrote:
my situation is that I must write a procedure stored so many reports entery pages. little enter pages use typed data groups and can't.
Called stored procedures what and where?
If, with a layer of app such as Java or .net, then they do not distinguish between weak and strong ref Cursor - because it is a concept of PL/SQL.
And by using a ref cursor type in PL/SQL is intended to provide a mechanism to pass the handle of real cursor SQL cursor created in this session, the client (for example Java or .net). Nothing more. So, why even care that PL/SQL? a weak or strong Ref to the application layer slider
so please tel me what is the best in terms of performance and maintainability above three conditions?
I find always frustrating that there is this concept that some methods to create cursors, results in faster and better than other methods, the sliders. It is a concept of bs. All SQLs are analyzed as a SQL cursors. Each. Unique. One. By the SAME CBO.
If the client code (Java, PL/SQL or other) you write for this SQL cursor DOES NOT change the source SQL.
This therefore does not cause the CBO now compile a different SQL execution plan for her. The CBO does not care if your code uses a strong Ref Cursor or a weak Ref Cursor, or a slider DBMS_SQL, an explicit cursor, or whatever. Those are concepts of customer. And do not change the execution plan of cursor SQL on the server. Or its planned performance/cost.
Yes, your client code (Java, PL/SQL, etc.) can use the SQL cursor on the server-side evil - and this can affect performance.
But waiting for a weak Ref cursor will somehow cause a faster cursor or slower for the same SQL statement, that a strong Ref cursor is not understanding what SQL cursor is, and how the customer interacts with its data types, the classes of customers and its customer interfaces with this SQL cursor.
-
Ref Cursor as an out parameter.
Hi all
Please help me with this.
It is a small procedure where the ref cursor is used as output parameter.
There is another procedure that calls the procedure above. But during the call, it is throwing an exception that ORA-01722. What's not here? Help, please.create or replace PROCEDURE P_REFCURSOR_OUT (P_DEPTNO IN EMP.DEPTNO%TYPE, P_RECORDSET OUT SYS_REFCURSOR) AS BEGIN OPEN P_RECORDSET FOR SELECT EMPNO, EMPNM, DEPTNO FROM EMP WHERE DEPTNO = P_DEPTNO ORDER BY EMPNM; END P_REFCURSOR_OUT;
Kind regardsCREATE OR REPLACE PROCEDURE P_CALL_REFCURSOR_OUT AS L_CURSOR SYS_REFCURSOR; L_EMPNM EMP.EMPNM%TYPE; L_EMPNO EMP.EMPNO%TYPE; L_DEPTNO EMP.DEPTNO%TYPE; BEGIN P_REFCURSOR_OUT (P_DEPTNO => 1000, P_RECORDSET => L_CURSOR); LOOP FETCH L_CURSOR INTO L_EMPNM, L_EMPNO, L_DEPTNO; EXIT WHEN L_CURSOR%NOTFOUND; DBMS_OUTPUT.PUT_LINE(L_EMPNM || ' | ' || L_EMPNO || ' | ' || L_DEPTNO); END LOOP; CLOSE L_CURSOR; END P_CALL_REFCURSOR_OUT;
BS2012The cursor that you open selects the EMPNO and DEPTNO EMPNM in that order.
When you retrieve the cursor, you get in L_EMPNM, L_EMPNO, L_DEPTNO.It would seem that the first two columns have been inverted in your FETCH statement. Assuming that EMPNO is a NUMBER EMPNM is a VARCHAR2, this means Oracle trying implicitly converted to the EMPNM in a NUMBER that is causing the error.
If you change the order of the variables in your FETCH statement, the error should disappear
FETCH L_CURSOR INTO L_EMPNO, L_EMPNM, L_DEPTNO;
Justin
-
The stored procedure PL/SQL - t - it accept the custom Ref Cursor type?
I am not able to compile the following procedure in the HR schema comes with default oracle... I use Oracle 11g Release 11.2.0.2.0 Express edition. It gives me an error (given after the code block):
Block of code:
CREATE OR REPLACE PROCEDURE TEST_REF IS
DECLARE
TYPE REF_EMP IS REF CURSOR RETURN % ROWTYPE EMPLOYEES;
RF_EMP REF_EMP;
V_EMP EMPLOYEES % ROWTYPE;
BEGIN
DBMS_OUTPUT. ENABLE (1000000);
OPEN FOR RF_EMP
SELECT * FROM EMPLOYEES WHERE EMPLOYEE_ID > 100;
EXTRACT THE RF_EMP IN V_EMP;
DBMS_OUTPUT. PUT_LINE (V_EMP. FIRST_NAME. ' ' || V_EMP. LAST_NAME);
CLOSE RF_EMP;
EXCEPTION
WHILE OTHERS
THEN DBMS_OUTPUT. PUT_LINE (SQLERRM);
END TEST_REF;
/
Error:
Errors in PROCEDURE TEST_REF:
LINE/COL ERROR
-------- -----------------------------------------------------------------
2/1 PLS-00103: encountered the symbol "DECLARE" when waiting for an a
What follows:
Start function < an ID > pragma procedure subtype type
< between double quote delimited identifiers > delete the current cursor
There are the external language prior
The symbol 'start' is substituted for 'DECLARE' continue.
16/13 PLS-00103: encountered the symbol "end-of-file" when waiting for him
one of the following values:
(begin case declare exit end exception for goto if loop mod)
pragma raise return null select update while withGet rid of the DECLARED:
SQL> CREATE OR REPLACE PROCEDURE TEST_REF IS 2 TYPE REF_EMP IS REF CURSOR RETURN HR.EMPLOYEES%ROWTYPE; 3 RF_EMP REF_EMP; 4 V_EMP EMPLOYEES%ROWTYPE; 5 BEGIN 6 DBMS_OUTPUT.ENABLE(1000000); 7 OPEN RF_EMP FOR 8 SELECT * FROM EMPLOYEES WHERE EMPLOYEE_ID > 100; 9 FETCH RF_EMP INTO V_EMP; 10 DBMS_OUTPUT.PUT_LINE(V_EMP.FIRST_NAME || ' ' || V_EMP.LAST_NAME); 11 CLOSE RF_EMP; 12 EXCEPTION 13 WHEN OTHERS 14 THEN DBMS_OUTPUT.PUT_LINE(SQLERRM); 15 END TEST_REF; 16 / Procedure created. SQL> set serveroutput on SQL> exec TEST_REF; Donald OConnell PL/SQL procedure successfully completed. SQL>
SY.
-
Hello all,.
I'm trying to implement a procedure that returns records in the hr.employees table when search strings are passed.
Here is the package:
The results I get are as follows:create or replace package search_app IS -- TYPE emp_rec IS RECORD (last_name hr.employees.last_name%TYPE ,first_name hr.employees.first_name%TYPE ,job_id hr.employees.first_name%TYPE ,salary hr.employees.salary%TYPE ,dept hr.departments.department_name%TYPE ); -- TYPE emp_curs IS REF CURSOR RETURN emp_rec; -- PROCEDURE get_employees (p_last_name_search IN hr.employees.last_name%TYPE ,p_first_name_search IN hr.employees.first_name%TYPE ,p_job_id_search IN hr.employees.job_id%TYPE ,pr_emps OUT emp_curs ,p_error OUT VARCHAR2 ); -- END search_app; / -- create or replace package body search_app IS -- PROCEDURE get_employees (p_last_name_search IN hr.employees.last_name%TYPE ,p_first_name_search IN hr.employees.first_name%TYPE ,p_job_id_search IN hr.employees.job_id%TYPE ,pr_emps OUT emp_curs ,p_error OUT VARCHAR2 ) IS -- v_last_name_search hr.employees.last_name%TYPE; v_first_name_search hr.employees.first_name%TYPE; v_job_id_search VARCHAR2(100); v_error_msg VARCHAR2(200); -- BEGIN -- OPEN pr_emps FOR -- SELECT e.last_name ,e.first_name ,e.job_id ,e.salary ,d.department_name as dept FROM hr.employees e ,hr.departments d WHERE (e.first_name LIKE p_first_name_search AND --OR e.last_name LIKE p_last_name_search AND --OR e.job_id IN p_job_id_search ) AND d.department_id = e.department_id; -- p_error := v_error_msg; -- EXCEPTION -- WHEN OTHERS THEN v_error_msg := SUBSTR(SQLERRM,1,200); END get_employees; -- END search_app; / sho error --- here is the stub I am using to test. -- set serveroutput on size 1000000 spool search_app_OR.log -- DECLARE -- v_first_name_search hr.employees.first_name%TYPE; v_last_name_search hr.employees.last_name%TYPE; v_job_id_search VARCHAR2(100); my_emps search_app.emp_curs; -- v_first_name hr.employees.first_name%TYPE; v_last_name hr.employees.last_name%TYPE; v_job_id VARCHAR2(100); v_salary hr.employees.salary%TYPE; v_dept_name hr.departments.department_name%TYPE; v_error_msg VARCHAR2(200); -- v_count PLS_INTEGER; -- BEGIN -- v_first_name_search := '%'; v_last_name_search := 'De%'; -- v_job_id_search := '%'; v_job_id_search := chr(40)||chr(39)||'AD_VP'||chr(39)||','||chr(39)||'SH_CLERK'||chr(39)||chr(41); -- search_app.get_employees(p_last_name_search => v_last_name_search ,p_first_name_search => v_first_name_search ,p_job_id_search => v_job_id_search ,pr_emps => my_emps ,p_error => v_error_msg ); -- LOOP -- FETCH my_emps INTO v_last_name, v_first_name,v_job_id, v_salary, v_dept_name; EXIT when my_emps%NOTFOUND; -- DBMS_OUTPUT.PUT_LINE ('...last_name : '||v_last_name); DBMS_OUTPUT.PUT_LINE ('...first_name: '||v_first_name); DBMS_OUTPUT.PUT_LINE ('...job_id : '||v_job_id); DBMS_OUTPUT.PUT_LINE ('...salary : '||v_salary); DBMS_OUTPUT.PUT_LINE ('...dept : '||v_dept_name); DBMS_OUTPUT.PUT_LINE ('...error_msg : '||v_error_msg); -- END LOOP; -- v_count := my_emps%ROWCOUNT; -- CLOSE my_emps; -- DBMS_OUTPUT.PUT_LINE ('v_last_name_search : '||v_last_name_search); DBMS_OUTPUT.PUT_LINE ('v_first_name_search : '||v_first_name_search); DBMS_OUTPUT.PUT_LINE ('v_job_id_search : '||v_job_id_search); DBMS_OUTPUT.PUT_LINE ('num rec fetched : '||v_count); -- EXCEPTION WHEN OTHERS THEN v_error_msg := SUBSTR(SQLERRM,1,200); DBMS_OUTPUT.PUT_LINE ('error: '||v_error_msg); END; / spool off
When I OR in the request for get_employees for search parameters, 106 records are returned.
When I AND in the application, not of records returned is 0. It should return 2 records. Why is it not return all records? Any suggestion on how this procedure should be applied?
Thank you
Raman
Published by: rxshah on June 8, 2010 12:52 AMHello
The problem is that you try to implement a dynamic list in a static SQL statement:
job_id IN '(''AD_VP'',''SH_CLERK'')'
It will not work like that.
Check out these links for some tips:
http://tkyte.blogspot.com/2006/06/varying-in-lists.html
http://asktom.Oracle.com/pls/asktom/f?p=100:11:1242571115131928:P11_QUESTION_ID:110612348061 -
REF cursor kicking package compilation error
I have a package with the following procedure:
It keeps giving me the error "encountered the symbol"REF"during awaits one of the following values:" any idea of another way to do this dynamic SQL cursor, or is there something I'm not here?PROCEDURE INTERPRET_DATAVRSN ( p_table IN VARCHAR2) AS cursor Generic_Tab IS REF CURSOR; c_sub Generic_Tab; v_SQL VARCHAR2(2000); v_SQLUpdate VARCHAR2(2000); v_RawDataVrsn VARCHAR2(200); BEGIN -- Start by selecting all of the unique values in the table. v_SQL := 'select distinct DataVrsn_in from '||p_table||' '; OPEN c_sub for v_SQL; -- Then process each unique value. Loop FETCH c_sub into v_RawDataVrsn; exit when c_sub %notfound; v_SQL := 'update '||p_table||' set DataVrsn='''; if (Instr(Upper(trim(v_RawDataVrsn)), 'V') > 0) then begin v_SQL := v_SQL||Replace(v_RawDataVrsn, 'V', '')||''' '; end else begin v_SQL := v_SQL||v_RawDataVrsn||''' '; end end if; v_SQL := v_SQL||''' where DataVrsn_in='''||v_RawDataVrsn||''' '; EXECUTE IMMEDIATE v_SQL; end loop; close c_sub; commit work; EXCEPTION WHEN OTHERS THEN /*DBMS_OUTPUT.PUT_LINE(' Received CODE=' || SQLCODE || ' MESSAGE=' || SQLERRM);*/ RAISE; END INTERPRET_DATAVRSN;
Thank you!Hello
It should be
type Generic_Tab IS REF CURSOR;
and is not
cursor Generic_Tab IS REF CURSOR;
In addition, your if condition does not have a begin and end, you can change it to
if (Instr(Upper(trim(v_RawDataVrsn)), 'V') > 0) then v_SQL := v_SQL||Replace(v_RawDataVrsn, 'V', '')||''' '; else v_SQL := v_SQL||v_RawDataVrsn||''' '; end if;
Arun-
-
Reg: Ref cursor in function
Hi friends,
LSPQ HELP ME.
I CREATED A FUNCTION AS EXAMPLE BELOW.
TYPE REFCURTYPE IS REFCURSOR; -DECLARE REF CURSOR.
CREATE FUNCTION FUNC1(EMPID,DESIG) RETURN REFCURTYPE
AS
OPEN FOR REFCURTYPE
SELECT A GROUP OF GROUPMASTER;
RETURN REFCURTYPE;
END;
WHEN I RUN THE QUERY AS
SELECT FUNC1('1001','SM') FROM DUAL;
IN THIS CASE, I WANT TO SEE THE REPORTS THAT IS COMES TO WORK IN REFCURTYPE... WHAT WOULD I DO?;Rajnish Chauhan says:
BUT EXPENSIVE...I CALL THIS QUERY IN THE PROCEDURE... SELECT FUNCTION('101','SM') FROM DUAL;
There is no need to SELECT. Just call function and fetch:
SQL> CREATE OR REPLACE 2 PROCEDURE P1 3 IS 4 v_cur SYS_REFCURSOR; 5 v_ename VARCHAR2(20); 6 BEGIN 7 v_cur := f1; 8 LOOP 9 FETCH v_cur INTO v_ename; 10 EXIT WHEN v_cur%NOTFOUND; 11 DBMS_OUTPUT.PUT_LINE(v_ename); 12 END LOOP; 13 END; 14 / Procedure created. SQL> set serveroutput on SQL> exec p1; SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER PL/SQL procedure successfully completed. SQL>
SY.
-
How to call a stored procedure with a REF CURSOR output parameter
I'm looking forward to example calling a function/stored procedure with a REF CURSOR output parameter and get the result.
In other words, I have a stored function/procedure that runs a SELECT statement using the OCI library and then he could get the values of each row and each column.
I put a code snippet, it have only the main thing to call a simple stored procedure and to print the name of each column of the cursor, but I couldn t to print out values in the table that calls the stored procedure.
I understand that the next step is to call an OCIStmtFetch.
How to associate the slider with the OCIStmtFetch?
If you need more information, just tell me.
I use ANSI C with HP - UX (HP - UX C) operating system and Oracle 10 g.
Kind regards.
Antonio Garcia
/ * callOracleSP * /.
#include < stdio.h >
#include < string.h >
#include < oci.h >
#include < stdlib.h > to
char * pConnectChar = "Server";
char * pUsernameChar = "user";
char * pPasswordChar = "passwd";
char * sqlCharArray1 = "BEGIN SP_GETCITIES (:,: c); END; « ;
int retval;
UB4 parmcnt = 0;
UB4 pos2 = 0;
text * pcoln [20];
UB4 namelen [20];
char state_key [5];
OCIStmt * pOciStatement;
OCIStmt * pOciStatCursor;
OCIError * pOciError;
OCIEnv * pOciEnviron;
OCIServer * pOciServer;
OCISession * pOciSession;
OCISvcCtx * pOciServiceContext;
OCIBind * pOciBind [500];
OCIParam * pOciParam;
int main()
{
retval = OCIEnvCreate (& pOciEnviron, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL);
retval = OCIEnvInit (& pOciEnviron, OCI_DEFAULT, 0, NULL);
retval = OCIHandleAlloc (pOciEnviron, (void *) & pOciError, OCI_HTYPE_ERROR, 0, NULL);
retval = OCIHandleAlloc (pOciEnviron, (void *) & pOciServiceContext, OCI_HTYPE_SVCCTX, 0, NULL);
retval = OCIHandleAlloc (pOciEnviron, (void *) & pOciStatement, OCI_HTYPE_STMT, 0, NULL);
retval = OCILogon (pOciEnviron, pOciError, & pOciServiceContext,(unsigned char *) pUsernameChar,
strlen (pUsernameChar), (unsigned char *) pPasswordChar, strlen (pPasswordChar).
(unsigned char *) pConnectChar, strlen (pConnectChar));
printf ("retval=%d\n",retval OCILogon);
retval = OCIStmtPrepare (pOciStatement, pOciError, (unsigned char *) sqlCharArray1, strlen (sqlCharArray1),)
OCI_NTV_SYNTAX, OCI_DEFAULT);
printf ("StmtPrepare retval=%d\n",retval);
retval = OCIHandleAlloc (pOciEnviron, (void *) & pOciStatCursor, OCI_HTYPE_STMT, 0, NULL);
retval = 1 OCIBindByPos(pOciStatement,&pOciBind[0], pOciError, (ub4), (void *) & state_key,)
((sb4) sizeof (state_key), SQLT_STR, (void *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT (ub4));
printf ("BindByPos OCI_HTYPE_STMT retval=%d\n",retval);
retval = OCIBindByPos(pOciStatement,&pOciBind[1], pOciError, (ub4) 2, (void *) & pOciStatCursor,)
((sb4) 0, SQLT_RSET, (void *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT (ub4));
printf ("BindByPos OCI_HTYPE_STMT retval=%d\n",retval);
strcpy (state_key, 'ca');
retval = OCIStmtExecute (pOciServiceContext, pOciStatement, pOciError, (ub4) 1, (ub4) 0,)
(OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT (ub4));
printf ("StmtExecute retval=%d\n",retval);
/ * How to get the values of the cursor? */
/ * Number of parameters of the cursor * /.
OCIAttrGet ((void *) pOciStatCursor, OCI_HTYPE_STMT (ub4), (void *) & parmcnt,(ub4 *) 0,)
(ub4) (OCI_ATTR_PARAM_COUNT, pOciError);
printf ("\nNumber of the slider settings = %d\n",parmcnt);
for (int pos = 1; pos < = (int) parmcnt; pos ++)
{
OCIAttrGet ((void *) pOciStatCursor, OCI_HTYPE_STMT (ub4), (void *) & pos2,(ub4 *) 0,)
(ub4) (OCI_ATTR_CURRENT_POSITION, pOciError);
retval = OCIParamGet ((void *) pOciStatCursor, OCI_HTYPE_STMT (ub4), pOciError, (void *) & pOciParam,)
POS (ub4));
OCIAttrGet pOciParam, (ub4) ((void*) OCI_DTYPE_PARAM,(void*) & pcoln [pos - 1],(ub4 *) & namelen [pos-1],)
(ub4) OCI_ATTR_NAME,(OCIError *) pOciError);
}
for (int i = 1; i < = (int) parmcnt; i ++)
printf ("%i\tNAME = % column. ("* s\n", i, namelen [i-1], pcoln [i-1]);
return 0;
}
This is the script that create the table, insert records and create the stored procedure
CREATE TABLE CITIES)
STATE_CODE VARCHAR2 (2) NULL,
CITY_CODE NUMBER (15.5) NULL,
CITY_NAME VARCHAR2 (30) NULL
)
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('CA', 30, 'SAN DIEGO')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('CA', 40 'SACRAMENTO')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('FL', 10, 'MIAMI')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('FL', 20, 'ORLANDO')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('NEW YORK', 10, 'NEW YORK')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('NEW YORK', 20, 'ALBANY')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('CA', 10, 'LOS ANGELES')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('CA', 20, 'SAN FRANCISCO')
/
CREATE or REPLACE PACKAGE globalPkg AUTHID CURRENT_USER AS
/ * The following is specific global variables T/SQL. */
TYPE RCT1 IS REF CURSOR; / * new cursor low definition * /.
END globalPkg;
/
CREATE OR REPLACE PROCEDURE SP_ADDCITY)
P_STATE_CODE IN VARCHAR,
P_CITY_CODE NUMBER,
P_CITY_NAME IN VARCHAR2,
P_RETURN IN NUMBERS)
AS
StoO_error INTEGER;
StoO_selcnt INTEGER;
StoO_rowcnt INTEGER;
StoO_errmsg VARCHAR2 (255);
BEGIN
StoO_rowcnt: = 0;
StoO_error: = 0;
StoO_selcnt: = 0;
P_RETURN: = 0;
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES (P_STATE_CODE, P_CITY_CODE, P_CITY_NAME);
StoO_rowcnt: = number of LINES SQL %;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
StoO_rowcnt: = 2;
WHILE OTHERS THEN
StoO_rowcnt: = 0;
StoO_selcnt: = 0;
StoO_error: = SQLCODE;
StoO_errmsg: = SQLERRM;
IF StoO_error! = 0 THEN
BEGIN
P_RETURN: = 1;
RETURN;
END;
END IF;
END;
/
CREATE OR REPLACE PROCEDURE SP_GETCITIES)
STATE_KEY IN VARCHAR,
RC1 IN OUT globalPkg.RCT1)
AS
StoO_error INTEGER;
StoO_selcnt INTEGER;
StoO_rowcnt INTEGER;
StoO_errmsg VARCHAR2 (255);
BEGIN
StoO_rowcnt: = 0;
StoO_error: = 0;
StoO_selcnt: = 0;
OPEN FOR RC1
SELECT STATE_CODE, CITY_CODE, FRANCISCO
CITIES
WHERE STATE_CODE = STATE_KEY
ORDER BY CITY_CODE;
StoO_rowcnt: = number of LINES SQL %;
EXCEPTION
WHILE OTHERS THEN
StoO_rowcnt: = 0;
StoO_error: = SQLCODE;
StoO_errmsg: = SQLERRM;
END;
/Hi Antonio,.
I see this:
c_buf=(ub1 **)calloc(sizeof(ub1 *),3); ... rc=OCIDefineByPos(pOciStatCursor,&pdef,(OCIError *)pOciError,pos,c_buf[pos-1],size+1,(ub2)type,(dvoid *)c_indp[pos-1],(ub2 *)0,(ub2 *)0,OCI_DEFAULT);
That I don't understand. You allocate space for 3 pointers ub1 but I don't see where these pointers are then initialized to point to where the data is to be stored.
I do not read correctly?
Sorry for posting code long, but here is an example of code that I have. It is much more 'code' for your code, but maybe that will be enough...
NOTE: This is just the code example and not rigorous. For example, I don't check the memory, allocations etc in this code!
Kind regards
Mark
#ifdef WIN32 #define _CRT_SECURE_NO_DEPRECATE 1 #endif #include
#include #include #include void checkerr(sword status, OCIError *errhp); int main(int argc, char *argv[]) { OCIEnv *envhp = NULL; /* OCI Environment handle */ OCIError *errhp = NULL; /* OCI Error handle */ OCISvcCtx *svchp = NULL; /* OCI Service Context handle */ OCIServer *srvhp = NULL; /* OCI Server handle */ OCISession *usrhp = NULL; /* OCI User Session handle */ OCIStmt *stmtp = NULL; /* OCI Statement handle */ OCIStmt *cursr = NULL; /* OCI Statement handle */ OCIParam *prmp1 = NULL; /* OCI Parameter handle */ OCIParam *prmp2 = NULL; /* OCI Parameter handle */ OCIParam *prmp3 = NULL; /* OCI Parameter handle */ OCIDefine *defp1 = NULL; /* OCI Define handle */ OCIDefine *defp2 = NULL; /* OCI Define handle */ OCIDefine *defp3 = NULL; /* OCI Define handle */ OCIBind *bndp1 = NULL; /* OCI Bind handle */ OCIBind *bndp2 = NULL; /* OCI Bind handle */ OCIBind *bndp3 = NULL; /* OCI Bind handle */ /* used to hold column width */ ub2 col_width; /* used to set the prefetch count */ ub4 prefetch_count = 32; /* will hold output from database */ oratext *pEmpId = NULL; oratext *pFirstName = NULL; oratext *pLastName = NULL; /* the anonymous block to execute */ /* this opens a ref cursor */ oratext *sqlstmt = "begin " \ " open :1 for " \ " select to_char(employee_id), " \ " first_name, " \ " last_name " \ " from hr.employees " \ " order by last_name, " \ " first_name; " \ "end;"; /* used to hold the results of each OCI call */ sword result = 0; /* Initialize and create a default environment */ result = OCIEnvCreate(&envhp, OCI_DEFAULT, (dvoid *) 0, 0, 0, 0, (size_t) 0, (dvoid **) 0); /* allocate an error handle */ result = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR, 0, (dvoid **) 0); /* allocate a service context handle */ result = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &svchp, OCI_HTYPE_SVCCTX, 0, (dvoid **) 0); /* allocate a server handle */ result = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &srvhp, OCI_HTYPE_SERVER, 0, (dvoid **) 0); /* allocate a user session handle */ result = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &usrhp, OCI_HTYPE_SESSION, 0, (dvoid **) 0); /* create a server context using the "ORADEMO" database */ result = OCIServerAttach(srvhp, errhp, "ORADEMO", (ub4) strlen("ORADEMO"), OCI_DEFAULT); /* set the server attribute in the service context handle */ result = OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *) srvhp, (ub4) 0, OCI_ATTR_SERVER, errhp); /* open the session with the database */ /* using external authentication */ result = OCISessionBegin(svchp, errhp, usrhp, OCI_CRED_EXT, OCI_DEFAULT); /* set the user session attribute in the service context handle */ result = OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *) usrhp, (ub4) 0, OCI_ATTR_SESSION, errhp); /* allocate the statement handle */ result = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &stmtp, OCI_HTYPE_STMT, 0, (dvoid **) 0); /* prepare the statement for execution */ result = OCIStmtPrepare(stmtp, errhp, sqlstmt, (ub4) strlen((char *) sqlstmt), OCI_NTV_SYNTAX, OCI_DEFAULT); /* allocate the handle for the ref cursor */ result = OCIHandleAlloc((dvoid *) envhp, (void **) &cursr, OCI_HTYPE_STMT, 0, NULL); /* bind the ref cursor parameter */ result = OCIBindByPos(stmtp, &bndp1, errhp, 1, &cursr, 0, SQLT_RSET, NULL, 0, NULL, 0, 0, OCI_DEFAULT); /* execute the statement */ result = OCIStmtExecute(svchp, stmtp, errhp, 1, 0, NULL, NULL, OCI_DEFAULT); /* get parameter descriptor for first column */ result = OCIParamGet((dvoid *) cursr, OCI_HTYPE_STMT, errhp, (dvoid **) &prmp1, (ub4) 1); /* get parameter descriptor for second column */ result = OCIParamGet((dvoid *) cursr, OCI_HTYPE_STMT, errhp, (dvoid **) &prmp2, (ub4) 2); /* get parameter descriptor for third column */ result = OCIParamGet((dvoid *) cursr, OCI_HTYPE_STMT, errhp, (dvoid **) &prmp3, (ub4) 3); /* get the first column width in characters */ result = OCIAttrGet((dvoid*) prmp1, (ub4) OCI_DTYPE_PARAM, (dvoid*) &col_width, (ub4 *) 0, (ub4) OCI_ATTR_DATA_SIZE, errhp); /* allocate memory to hold the result */ pEmpId = (oratext *) malloc(sizeof(oratext) * (col_width + 1)); /* define the first column in the results */ result = OCIDefineByPos(cursr, &defp1, errhp, 1, (dvoid *) pEmpId, (sword) col_width + 1, SQLT_STR, (dvoid *) NULL, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT); /* get the second column width in characters */ result = OCIAttrGet((dvoid*) prmp2, (ub4) OCI_DTYPE_PARAM, (dvoid*) &col_width, (ub4 *) 0, (ub4) OCI_ATTR_DATA_SIZE, errhp); /* allocate memory to hold the result */ pFirstName = (oratext *) malloc(sizeof(oratext) * (col_width + 1)); /* define the second column in the results */ result = OCIDefineByPos(cursr, &defp2, errhp, 2, (dvoid *) pFirstName, (sword) col_width + 1, SQLT_STR, (dvoid *) NULL, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT); /* get the third column width in characters */ result = OCIAttrGet((dvoid*) prmp3, (ub4) OCI_DTYPE_PARAM, (dvoid*) &col_width, (ub4 *) 0, (ub4) OCI_ATTR_DATA_SIZE, errhp); /* allocate memory to hold the result */ pLastName = (oratext *) malloc(sizeof(oratext) * (col_width + 1)); /* define the third column in the results */ result = OCIDefineByPos(cursr, &defp3, errhp, 3, (dvoid *) pLastName, (sword) col_width + 1, SQLT_STR, (dvoid *) NULL, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT); /* loop through and print the results */ while ((result = OCIStmtFetch(cursr, errhp, (ub4) 1, (ub2) OCI_FETCH_NEXT, (ub4) OCI_DEFAULT)) == OCI_SUCCESS) { printf("Employee ID: %s\n", pEmpId); printf(" First Name: %s\n", pFirstName); printf(" Last Name: %s\n\n", pLastName); } /* free allocated memory */ free(pEmpId); free(pFirstName); free(pLastName); pEmpId = NULL; pFirstName = NULL; pLastName = NULL; /* terminate the session with the database */ result = OCISessionEnd(svchp, errhp, usrhp, OCI_DEFAULT); /* detach from the server */ result = OCIServerDetach(srvhp, errhp, OCI_DEFAULT); /* deallocate the environment handle */ /* OCI will deallocate the child handles */ result = OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV); return OCI_SUCCESS; } void checkerr(sword status, OCIError *errhp) { oratext errbuf[512]; sb4 errcode = 0; switch (status) { case OCI_SUCCESS: break; case OCI_ERROR: case OCI_SUCCESS_WITH_INFO: (void) OCIErrorGet((dvoid *) errhp, (ub4) 1, (oratext *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR); (void) printf("Error: %.*s\n", sizeof(errbuf), errbuf); break; case OCI_NEED_DATA: (void) printf("Error - OCI_NEED_DATA\n"); break; case OCI_NO_DATA: (void) printf("Error - OCI_NO_DATA\n"); break; case OCI_INVALID_HANDLE: (void) printf("Error - OCI_INVALID_HANDLE\n"); break; case OCI_STILL_EXECUTING: (void) printf("Error - OCI_STILL_EXECUTING\n"); break; case OCI_CONTINUE: (void) printf("Error - OCI_CONTINUE\n"); break; default: break; } }
Maybe you are looking for
-
problems with the latest Firefox
I am very satisfied with the latest version of Firefox!The following problems occurred that will never happen to me using Firefox before: 1 I started to see some blurry text characters sometimes. I thought that my video card was dying, but it does no
-
Windows sounds users randomly to update slow computers
I get phone calls from 'windows' with foreign voices asking me to update my computer while they are on the phone because my computer is dangerous with many apps that slow down and they'll talk me through to change that. Is this a real phone call fro
-
Kingston Hyperx SH100S3 / 240G 2.5 "SATA III Solid State Drive
Before making a purchase this SSD will work in my Lenovo ThinkPad T410 2516ADU. Thank you.
-
I have an ASUS computer (actually it's a DIY chassis and motherboard ASUS with CD/DVD player and it is a real improvement over what I used, but I want to upgrade a NVidia 6200 AGPto graphics card of PRO for an improvement in Visual acuity and a clean
-
I want to make my printer HP Officejet 4620 to another computer. Now, the other computer has Windows 7 and Canon i350 printer. I won't use the cannon. Do I need to use the installation disk supplied with the HP printer?