Upper Function is not working in out cursor - oracle

I tried to execute the select query with UPPER function that's working fine, while trying to execute through out cursor in oracle, it's not working. kindly tell what i am did wrong here?
QUERY:
SELECT ISSUEID,CATEGORY_1,CATEGORY_2,CATEGORY_3 FROM ISSUES
WHERE UPPER(CATEGORY_2)=UPPER('ORDERNAME');
OUT CURSOR:
OPEN OUT_CURSOR FOR SELECT ISSUEID,CATEGORY_1,CATEGORY_2,CATEGORY_3 FROM ISSUES
WHERE UPPER(CATEGORY_2)=UPPER('ORDERNAME');
Complete Procedure:
PROCEDURE ISSUE_SEARCH(
IN_ISSUEID IN NUMBER DEFAULT NULL,
IN_ORDERJOURNEY IN NVARCHAR2 DEFAULT NULL,
OUT_CURSOR OUT SYS_REFCURSOR
)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE('INSIDE IF..');
OPEN OUT_CURSOR FOR SELECT ISSUEID,CATEGORY_1,CATEGORY_2,CATEGORY_3, FROM ISSUES
WHERE UPPER(CATEGORY_2)=UPPER('IN_ORDERJOURNEY');
EXCEPTION
WHEN NO_DATA_FOUND THEN
OUT_CURSOR :=null;
END ISSUE_SEARCH;

Related

ORA-01001: invalid cursor . Passing cursor to a procedure

When I execute the code below I got ORA-01001: invalid cursor and ORA-06512 error. It was not always the case.I thought I pass the cursor to MY_READ procedure by reference and can close it there. But as I started to get the error above suddenly I concluded, that maybe I'm not passing a reference but the copy of the cursor. Am I right? Is it possible to read close the cursor in MY_READ function?
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PROCEDURE REP_HELPER1 (myIdx IN BINARY_INTEGER, from_d IN DATE, rep_table IN OUT rep_table_T) IS
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
myCUR1 SYS_REFCURSOR;
BEGIN
OPEN myCUR1 FOR SELECT myField1,
myField2,
myField3,
myField4,
myField5,
myField6,
myField7,
myField8,
myField9,
myField10,
myField11,
myField12,
myField13,
myField14,
myField15,
myField16,
myField17,
myField18,
myField19,
myField20,
myField21,
myField22,
myField23,
myField24,
myField25,
myField26,
myField27,
myField28,
myField29,
myField30,
myField31
FROM myTable;
MY_READ(myIdx , myCUR1, rep_table)
END REP_HELPER1;
--Am I passing here a copy of a cursor and not a reference?
PROCEDURE MY_READ(myIdx IN BINARY_INTEGER, cur IN SYS_REFCURSOR, rep_table IN OUT rep_table_T) IS
BEGIN
FETCH cur INTO rep_table(myIdx).day1, rep_table(myIdx).day2, rep_table(myIdx).day3, rep_table(myIdx).day4, rep_table(myIdx).day5,
rep_table(myIdx).day6, rep_table(myIdx).day7, rep_table(myIdx).day8, rep_table(myIdx).day9, rep_table(myIdx).day10,
rep_table(myIdx).day11, rep_table(myIdx).day12, rep_table(myIdx).day13, rep_table(myIdx).day14, rep_table(myIdx).day15,
rep_table(myIdx).day16, rep_table(myIdx).day17, rep_table(myIdx).day18, rep_table(myIdx).day19, rep_table(myIdx).day20,
rep_table(myIdx).day21, rep_table(myIdx).day22, rep_table(myIdx).day23, rep_table(myIdx).day24, rep_table(myIdx).day25,
rep_table(myIdx).day26, rep_table(myIdx).day27, rep_table(myIdx).day28, rep_table(myIdx).day29, rep_table(myIdx).day30,
rep_table(myIdx).day31;
IF cur%NOTFOUND THEN -- here comes ORA-06512 in a stack
dbms_output.put_line('ERROR' || nIndex);
END IF;
CLOSE cur;
END MY_READ;
When I do FETCH and close in REP_HELPER1 I'm not getting the error.
This simple example seems equivalent to yours and works fine:
declare
mycur sys_refcursor;
procedure my_read (cur sys_refcursor) is
job long;
sal number;
begin
fetch cur into job, sal;
dbms_output.put_Line(job||' '||sal);
close cur;
end;
begin
open mycur for select job, sal from emp where ename = 'KING';
my_read (mycur);
end;
So I don't think that is the issue.
According to https://www.techonthenet.com/oracle/errors/ora01001.php:
You tried to reference a cursor that does not yet exist. This may have
happened because:
You've executed a FETCH cursor before OPENING the cursor.
You've executed a CLOSE cursor before OPENING the cursor.
You've executed a FETCH cursor after CLOSING the cursor.

ORA-01002: fetch out of sequence in procedure

I created a procedure in oracle database that returns data in a ref-cursor, and I want it to return the rowcount of this cursor also as an output variable. After testing, the P_count variable is filled correctly, but when I tried to open it an
ORA-01002: fetch out of sequence
error is fired. I have read before about it and I found that the problem is because I am using a fetch statement in. But till now I did not discover how to resolve it. Any helps are appreciated, thank you.
Below is my Procedure:
PROCEDURE IS_CLIENT_LOGGED_IN (P_CLIENT_NUM Varchar2,P_CURSOR out SYS_REFCURSOR ,P_COUNT OUT NUMBER,P_ERROR out Varchar2) AS
TYPE MyRec IS RECORD (ID VARCHAR2(100));
cur_rec MyRec;
lv_cur SYS_REFCURSOR;
BEGIN
BEGIN
Open lv_cur FOR
SELECT ID
FROM tbl_registration
WHERE tbl_client_id = P_CLIENT_NUM
AND tbl_logout_date is null;
LOOP
FETCH lv_cur INTO cur_rec;
EXIT WHEN P_CURSOR%notfound;
P_COUNT := P_CURSOR%rowcount;--will return row number beginning with 1
END LOOP;
P_CURSOR := lv_cur;
EXCEPTION WHEN OTHERS THEN
P_ERROR := 'Unable to select Data from tbl_registration' ||SQLERRM;
END;
Searching online I found reasons for the issue as below:
Fetching from a cursor after the last row has been retrieved and the ORA-1403 error returned.
If the cursor has been opened with the FOR UPDATE clause, fetching after a COMMIT has been issued will return the error.
Rebinding any placeholders in the SQL statement, then issuing a fetch before reexecuting the statement.
But I cannot find a proper solution.
You can only use a cursor once, so if you want both the row count and the actual result set, you will need two separate queries. Then the problem is that the data might have been updated by other sessions between the two queries, giving inconsistent results. (For example, another session inserted some rows earlier, and then commits while your procedure is running.)
One approach would be to use flashback query to ensure that the count and the fetch refer to the same point in time:
create or replace procedure is_client_logged_in
( p_client_num varchar2
, p_cursor out sys_refcursor
, p_count out number
, p_error out varchar2 )
as
k_starttime constant timestamp := systimestamp;
begin
select count(*) into p_count
from tbl_registration as of timestamp k_starttime
where tbl_client_id = p_client_num
and tbl_logout_date is null;
open p_cursor for
select id
from tbl_registration as of timestamp k_starttime
where tbl_client_id = p_client_num
and tbl_logout_date is null;
exception
when others then p_error := 'Unable to retrieve registration data.'||chr(10)||sqlerrm;
end;
What you want sounds very simple, but isn't. A cursor is for one-use only. So if you read through it, you are done with it. Returning the unused cursor plus its row count requires special handling hence.
One way to come to mind is two separate queries: One to count the rows, one to open the cursor. The problem, though: In the nanoseconds between the queries the row count may change because of other sessions inserting or deleting or updating data. Okay, you can lock the table exclusively, but then you will have to release it after the two statements. Unfortunately, there is no UNLOCK TABLE command in Oracle. Releasing a table is only possible via a COMMIT or ROLLBACK. So, in order not to interfere with the caller's transaction you need a SAFEPOINT, too.
Here is your procedure with the above method applied:
CREATE OR REPLACE PROCEDURE is_client_logged_in (p_client_num VARCHAR2, p_cursor OUT SYS_REFCURSOR, p_count OUT NUMBER, p_error OUT VARCHAR2) AS
BEGIN
SAVEPOINT for_unlock;
LOCK TABLE tbl_registration IN EXCLUSIVE MODE;
SELECT COUNT(*) INTO p_count
FROM tbl_registration
WHERE tbl_client_id = p_client_num
AND tbl_logout_date IS NULL;
Open p_cursor FOR
SELECT id
FROM tbl_registration
WHERE tbl_client_id = p_client_num
AND tbl_logout_date IS NULL;
ROLLBACK TO SAVEPOINT for_unlock;
EXCEPTION WHEN OTHERS THEN
ROLLBACK TO SAVEPOINT for_unlock;
p_error := 'Unable to select Data from tbl_registration' || SQLERRM;
END is_client_logged_in;
Demo: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=552d7f76d03a9a221c17f05c6663d2c9
Here is an alternative. It's really muddling through, but well...
I've shown a solution with locking the table. But imagine a query that runs for an hour. You'd lock that table for twice this time.
William has shown a great solution with flashback queries. But with a query running for an hour, the flashback data may time out.
So, here is a weird solution getting count and results in one query. This is easy in your case, because I expect the ID you are selcting to be an integer, just as the count. So I select the count into the first row, fetch that and give back the cursor that is now at the position for the first actual data row (the first ID).
CREATE OR REPLACE PROCEDURE is_client_logged_in (p_client_num VARCHAR2, p_cursor OUT SYS_REFCURSOR, p_count OUT NUMBER, p_error OUT VARCHAR2) AS
BEGIN
Open p_cursor FOR
WITH data AS
(
SELECT id
FROM tbl_registration
WHERE tbl_client_id = p_client_num
AND tbl_logout_date IS NULL
)
SELECT id
FROM
(
SELECT 1 as sortkey, COUNT(*) AS id FROM data
UNION ALL
SELECT 1 as sortkey, id FROM data
)
ORDER BY sortkey;
FETCH p_cursor INTO p_count;
EXCEPTION WHEN OTHERS THEN
p_error := 'Unable to select Data from tbl_registration' || SQLERRM;
END is_client_logged_in;
Demo: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=24ed333b9c1ed700af6d3a486ef47ccd

expression of wrong type oracle error

I am trying to execute the below plsql program, but facing expression of wrong type. Could anyone let me know what might be the error?
CREATE OR REPLACE PROCEDURE CLN_TBL (CTRLM IN VARCHAR2, CTG IN VARCHAR,SBCT IN NUMBER, RTDT IN NUMBER )
AS
V_SQL VARCHAR(2000);
V_TABLE VARCHAR(30);
CURSOR TBL_CUR
IS
SELECT TGT_TABLE_NAME FROM ODS_USER.CLNP WHERE CONTROLM=CTRLM AND APPL_CTGY=CTG AND APPL_SUB_CTGY= SBCT;
L_TGT_TABLE_NAME TBL_CUR%ROWTYPE;
BEGIN
OPEN TBL_CUR;
LOOP
FETCH TBL_CUR INTO L_TGT_TABLE_NAME;
V_TABLE:= L_TGT_TABLE_NAME ;
EXIT WHEN TBL_CUR%NOTFOUND;
V_SQL:='DELETE FROM '||V_TABLE||' WHERE RPT_DT_ID'||'=:1';
EXECUTE IMMEDIATE V_SQL using RTDT;
END LOOP;
COMMIT;
CLOSE TBL_CUR;
END;
As Exhausted said you cant assign row variable to varchar so You should take TGT_TABLE_NAME from row variable, like below should work;
CREATE OR REPLACE PROCEDURE CLN_TBL (CTRLM IN VARCHAR2, CTG IN VARCHAR,SBCT IN NUMBER, RTDT IN NUMBER )
AS
V_SQL VARCHAR(2000);
V_TABLE VARCHAR(30);
CURSOR TBL_CUR
IS
SELECT TGT_TABLE_NAME FROM ODS_USER.CLNP WHERE CONTROLM=CTRLM AND APPL_CTGY=CTG AND APPL_SUB_CTGY= SBCT;
L_TGT_TABLE_NAME TBL_CUR%ROWTYPE;
BEGIN
OPEN TBL_CUR;
LOOP
FETCH TBL_CUR INTO L_TGT_TABLE_NAME;
V_TABLE:= L_TGT_TABLE_NAME.TGT_TABLE_NAME ;
EXIT WHEN TBL_CUR%NOTFOUND;
V_SQL:='DELETE FROM '||V_TABLE||' WHERE RPT_DT_ID'||'=:1';
EXECUTE IMMEDIATE V_SQL using RTDT;
END LOOP;
COMMIT;
CLOSE TBL_CUR;
END;

dbms_sql.to_cursor_number - Getting Invalid Cursor error for SYS_REFCURSOR

I have the following code for table and view creation.
create table test_company
(
comp_id number
, comp_name varchar2(500)
)
;
insert into test_company values(1, 'CompanyA');
insert into test_company values(2, 'CompanyB');
insert into test_company values(3, 'CompanyC');
create or replace view test_company_view as select * from test_company;
And I have the following code for my cursor testing. But dbms_sql.to_cursor_number got error ORA-01001: invalid cursor
set serveroutput on;
declare
reader test_company_view%ROWTYPE;
datacursor SYS_REFCURSOR;
v_cursor_id number;
begin
open datacursor for select * from test_company_view;
v_cursor_id := dbms_sql.to_cursor_number(datacursor); -- ERROR: invalid cursor
loop fetch datacursor into reader;
exit when datacursor%NOTFOUND;
dbms_output.put_line(reader.comp_id);
end loop;
close datacursor;
end;
What did I do wrong? Thank you for your helps!
I have tried strongly-typed REF CURSOR and weakly-typed REF CURSOR but they got the same error.
From the documentation:
After you convert a REF CURSOR variable to a SQL cursor number, native dynamic SQL operations cannot access it.
It is not dbms_sql.to_cursor_number that is raising the error, it is fetch datacursor into reader since datacursor can no longer be accessed.

Dynamic SQL and out field in oracle procedure

I get error when I use this:
PROCEDURE GET_BY_CRIT(vchFilter varchar2(500),
intCantTotal OUT INT,
curResult OUT sys_refcursor)
IS
BEGIN
OPEN curResult FOR
'SELECT COLUMN1,COLUMN2 FROM SOME_TABLE WHERE '||vchFilter
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM SOME_TABLE WHERE '||vchFilter
INTO intCantTotal
END
Error:
ORA-00936: missed expression
But when I execute each sentence by separate it run correcly.
The error you're getting doesn't seem to make sense. Oracle should be throwing a compilation error because parameters to functions don't have a length. vchFilter should be declared as a VARCHAR2, not a VARCHAR2(500).
Additionally, as Lolo pointed out in the comments, statements in a PL/SQL block need to be terminated with semicolons.
PROCEDURE GET_BY_CRIT(vchFilter varchar2,
intCantTotal OUT integer,
curResult OUT sys_refcursor)
IS
BEGIN
OPEN curResult FOR
'SELECT COLUMN1,COLUMN2 FROM SOME_TABLE WHERE '||vchFilter;
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM SOME_TABLE WHERE '||vchFilter
INTO intCantTotal;
END;
Be aware as well that there is no guarantee that second SQL statement will see the same COUNT that the first SQL statement did unless you can guarantee that SOME_TABLE is not being modified by any other sessions at the same time you're querying it. I'd generally be rather wary of a need to run a query and execute a separate count-- that generally indicates a more basic problem. If you need the COUNT to be consistent with the query you're running, you'd want to add an analytic COUNT to your query and let the caller fetch that column.
PROCEDURE GET_BY_CRIT(vchFilter varchar2,
curResult OUT sys_refcursor)
IS
BEGIN
OPEN curResult FOR
'SELECT COLUMN1,COLUMN2, COUNT(*) OVER () cnt FROM SOME_TABLE WHERE '||vchFilter;
END;

Resources