Home » SQL & PL/SQL » SQL & PL/SQL » ora-00900 invalid sql statement while using open cursor for variable
ora-00900 invalid sql statement while using open cursor for variable [message #641985] Fri, 28 August 2015 13:41 Go to next message
bhardkap
Messages: 2
Registered: August 2015
Junior Member
Hi Tech Gurus,
I have one requirement in which i have to fetch the select query from a table which i'll use to generate a report using pl/sql stored procedure.
i tried doing it using ref cursor as follows:( please note that the below code is just a sample code of my actual code)

Create or replace PROCEDURE PROC1
AS
TYPE T_CURSOR IS REF CURSOR;
v_query patent.query%type;
Proc_nm patent.procedure_name%type;
VID patent.ID%type;
a varchar:= '';
b varchar:='';
CURSOR C
IS
SELECT Query,
FROM patent where flag=1;
cur_ref T_CURSOR;
BEGIN
open C;
loop
fetch c into V_query;
exit when c%notfound;
end loop;

OPEN cur_ref FOR v_query ; --------- error statement of executable code
LOOP
FETCH cur_ref bulk INTO a,b,c;
EXIT
WHEN cur_ref%NOTFOUND;

END LOOP;
close c;
END PROC1;

-- query is the column of the patent table and will contain a generic query like select x,y,z from sample;

The above code compiled successfully. but while executing the code i got an error ::ora-00900 invalid sql statement" .
my question is :can we use v_query for fetching the rows as mentioned above ? as i can't directly hardcode the query in place of v_query as it should come from the table itself.

Please provide your suggestions.
Re: ora-00900 invalid sql statement while using open cursor for variable [message #641986 is a reply to message #641985] Fri, 28 August 2015 13:56 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

v_query is "v_query patent.query%type;"
we don't know anything about patent, patent.query%type and what data are inside it so we can't answer.

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Also always post your Oracle version, with 4 decimals.

With any SQL or PL/SQL question, please, Post a working Test case: create statements for all objects so that we will be able work to reproduce what you have.

[Updated on: Fri, 28 August 2015 13:57]

Report message to a moderator

Re: ora-00900 invalid sql statement while using open cursor for variable [message #641987 is a reply to message #641985] Fri, 28 August 2015 13:56 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read

> as i can't directly hardcode the query in place of v_query as it should come from the table itself.
This "design" is daft.
>FETCH cur_ref bulk INTO a,b,c;
what happens when not three columns are returned?
What happens when returned values are not VARCHAR2?

Re: ora-00900 invalid sql statement while using open cursor for variable [message #641989 is a reply to message #641986] Fri, 28 August 2015 13:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Also: "FETCH cur_ref bulk INTO a,b,c;"
But C is defined as "CURSOR C IS SELECT Query,..."
So C is a constant and can't receive any value.

Also A is defined as "a varchar:= '';"
So you can't BULK COLLECT INTO a as BULK COLLECT implies an array to receive it.

Re: ora-00900 invalid sql statement while using open cursor for variable [message #641990 is a reply to message #641989] Fri, 28 August 2015 14:00 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

And not that VARCHAR should not be used, use VARCHAR2 instead.

In the end post a code that really represents your problem, which means a code that compiles which is not the case of yours.

Re: ora-00900 invalid sql statement while using open cursor for variable [message #641997 is a reply to message #641990] Sat, 29 August 2015 02:50 Go to previous messageGo to next message
bhardkap
Messages: 2
Registered: August 2015
Junior Member
Hi,
Thanks for the response. but as i already mentioned in my last post that i just showed you my sample code and not the actual code .Please find below the code with minor correction and do reply for the errors which i got while executing it. my question is can i use a variable (v_query) for opening the cursor instead of writing the full query in the procedure itself as the actual query which is coming the this variable from the patent table.
Patent table is as follows:
id query procedure_name flag
1 select a,b,c from sample; proc1 1 -- where a is number,b is varchar2,c is varchar2
2 select a,b,c from sample1; proc2 0
Create or replace PROCEDURE PROC1
AS
TYPE T_CURSOR IS REF CURSOR;
v_query patent.query%type;
Proc_nm patent.procedure_name%type;
VID patent.ID%type;
a number:= '';
b varchar2(500):='';
c varchar2(500):='';
CURSOR C
IS
SELECT Query
FROM patent where flag=1;
cur_ref T_CURSOR;
BEGIN
open C;
loop
fetch c into V_query;
exit when c%notfound;
end loop;

OPEN cur_ref FOR v_query ; --------- error statement of executable code
LOOP
FETCH cur_ref INTO a,b,c;
EXIT
WHEN cur_ref%NOTFOUND;

END LOOP;
close c;
END PROC1;
Re: ora-00900 invalid sql statement while using open cursor for variable [message #641998 is a reply to message #641997] Sat, 29 August 2015 04:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

As I said in my previous posts you MUST post a test case which 1) represents your problem and 2) can be reproduced by us.
Also follow the guide lines and format your code (indent it) and use SQL*Plus and copy and paste your session and post your Oracle version...
Why are you so reluctant to post what we ask?
Do you really expect we help you when you don't respect us.
We are not here to serve you.

Re: ora-00900 invalid sql statement while using open cursor for variable [message #642118 is a reply to message #641998] Wed, 02 September 2015 22:44 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
I think this is your req.

CREATE TABLE PATENT (ID  NUMBER, QUERY VARCHAR2(90),PROCEDURE_NAME VARCHAR2(90),FLAG VARCHAR2(90));
INSERT INTO  PATENT VALUES (101,'SAMPLE_QUERY','PRC1','1'); 
COMMIT;

CREATE OR REPLACE
PROCEDURE PROC1
AS
TYPE T_CURSOR
IS
  REF
  CURSOR;
    V_QUERY PATENT.QUERY%TYPE;
    PROC_NM PATENT.PROCEDURE_NAME%TYPE;
    VID PATENT.ID%TYPE;
    CURSOR C
    IS
      SELECT QUERY FROM PATENT WHERE FLAG=1;
    CUR_REF T_CURSOR;
  BEGIN
    OPEN C;
    LOOP
      FETCH C INTO V_QUERY;
      EXIT
    WHEN C%NOTFOUND;
      DBMS_OUTPUT.PUT_LINE('Using normal cursor : '||V_QUERY);
    END LOOP;
    CLOSE C;
    OPEN CUR_REF FOR SELECT QUERY FROM PATENT WHERE FLAG=1;
    LOOP
      FETCH CUR_REF INTO V_QUERY;
      EXIT
    WHEN CUR_REF%NOTFOUND;
      DBMS_OUTPUT.PUT_LINE('Using Refcursor : '||V_QUERY);
    END LOOP;
    CLOSE C;
  END PROC1;
Re: ora-00900 invalid sql statement while using open cursor for variable [message #642143 is a reply to message #642118] Thu, 03 September 2015 12:20 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
There is no chance that's what the OP wants. OP is trying to execute dynamic queries stored in a table, your code doesn't do that
Re: ora-00900 invalid sql statement while using open cursor for variable [message #642168 is a reply to message #642143] Fri, 04 September 2015 05:49 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
CREATE TABLE EMP_1 (EMPNO NUMBER PRIMARY KEY, ENAME VARCHAR2(90), DEPTNO NUMBER REFERENCING DEPT_1(DEPTNO));
  CREATE TABLE DEPT_1 (DEPTNO NUMBER PRIMARY KEY, DEPT_NAME VARCHAR2(90));
  CREATE TABLE SALARY_1 (EMPNO NUMBER REFERENCING EMP_1(EMPNO), SALARY NUMBER);
  INSERT INTO EMP_1 VALUES (101, 'ADAMN', 1);
  INSERT INTO EMP_1 VALUES (102, 'RICKY', 2);
  INSERT INTO EMP_1 VALUES (103, 'CLARK', 3);
  INSERT INTO EMP_1 VALUES (104, 'PATIL', 2);
  
  INSERT INTO DEPT_1 VALUES (1, 'MANAGER');
  INSERT INTO DEPT_1 VALUES (2, 'ACCOUNT');
  INSERT INTO DEPT_1 VALUES (3, 'HR');
 
  INSERT INTO SALARY_1 VALUES (101, 10000);
  INSERT INTO SALARY_1 VALUES (102, 2400);
  INSERT INTO SALARY_1 VALUES (103, 5000);
  INSERT INTO SALARY_1 VALUES (104, 1500);

CREATE TABLE PATENT (ID NUMBER, QUERY VARCHAR2(1000), PROC VARCHAR2(100),FLG VARCHAR2(1));
                       DROP TABLE PATENT;
  
  INSERT INTO PATENT VALUES  (10,'SELECT EMPNO, ENAME, DEPTNO FROM EMP_1','PRC1','1');
  INSERT INTO PATENT VALUES  (20,'SELECT DEPTNO, DEPT_NAME FROM DEPT_1','PRC2','1');
  INSERT INTO PATENT VALUES  (30,'SELECT EMPNO, SALARY FROM SALARY_1','PRC3','1');
/
create or replace PROCEDURE PROC1 AS
  TYPE T_CURSOR IS REF CURSOR;
  CUR_REF T_CURSOR;
  
  TYPE REC_EMP IS RECORD (EMPNO EMP_1.EMPNO%TYPE, ENAME EMP_1.ENAME%TYPE, DEPTNO EMP_1.DEPTNO%TYPE);
  LV_REC_EMP REC_EMP;
  TYPE REC_DEPT IS RECORD (DEPTNO DEPT_1.DEPTNO%TYPE, DEPT_NAME DEPT_1.DEPT_NAME%TYPE );
  LV_REC_DEPT REC_DEPT;
  TYPE REC_SAL IS RECORD (EMPNO SALARY_1.EMPNO%TYPE, SALARY SALARY_1.SALARY%TYPE);
  LV_REC_SAL REC_SAL;
  
  CURSOR C1 IS SELECT QUERY,PROC FROM PATENT WHERE FLG='1';
  V_QUERY PATENT.QUERY%TYPE;
  PROC PATENT.PROC%TYPE;
  
BEGIN
    OPEN C1;
    LOOP
      FETCH C1 INTO V_QUERY,PROC;
      EXIT WHEN C1%NOTFOUND;
          DBMS_OUTPUT.PUT_LINE('Using normal cursor : '||V_QUERY);
          OPEN CUR_REF FOR V_QUERY;
          LOOP
          IF PROC ='PRC1' THEN
              FETCH CUR_REF INTO LV_REC_EMP;
              EXIT WHEN CUR_REF%NOTFOUND;
              DBMS_OUTPUT.PUT_LINE('Using Refcursor : '||LV_REC_EMP.EMPNO||' | '||LV_REC_EMP.ENAME||' | '||LV_REC_EMP.DEPTNO);
          ELSIF PROC ='PRC2' THEN
              FETCH CUR_REF INTO LV_REC_DEPT;
              EXIT WHEN CUR_REF%NOTFOUND;
              DBMS_OUTPUT.PUT_LINE('Using Refcursor : '||LV_REC_DEPT.DEPTNO||' | '||LV_REC_DEPT.DEPT_NAME);
          ELSIF PROC ='PRC3' THEN
              FETCH CUR_REF INTO LV_REC_SAL;
              EXIT WHEN CUR_REF%NOTFOUND;
              DBMS_OUTPUT.PUT_LINE('Using Refcursor : '||LV_REC_SAL.EMPNO||' | '||LV_REC_SAL.SALARY);
          END IF;
          END LOOP;
    END LOOP;
    CLOSE C1;
  END PROC1;
/
--Output for the above procedure
Using normal cursor : SELECT EMPNO, ENAME, DEPTNO FROM EMP_1
Using Refcursor : 101 | ADAMN | 1
Using Refcursor : 102 | RICKY | 2
Using Refcursor : 103 | CLARK | 3
Using Refcursor : 104 | PATIL | 2
Using normal cursor : SELECT DEPTNO, DEPT_NAME FROM DEPT_1
Using Refcursor : 1 | MANAGER
Using Refcursor : 2 | ACCOUNT
Using Refcursor : 3 | HR
Using normal cursor : SELECT EMPNO, SALARY FROM SALARY_1
Using Refcursor : 101 | 10000
Using Refcursor : 102 | 2400
Using Refcursor : 103 | 5000
Using Refcursor : 104 | 1500
Re: ora-00900 invalid sql statement while using open cursor for variable [message #642173 is a reply to message #642168] Fri, 04 September 2015 08:28 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
It appears OP was write once & return never.
Re: ora-00900 invalid sql statement while using open cursor for variable [message #642174 is a reply to message #642173] Fri, 04 September 2015 08:37 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
Return Means What???...Using above procedure we can run the query dynamically.
Re: ora-00900 invalid sql statement while using open cursor for variable [message #642175 is a reply to message #642174] Fri, 04 September 2015 08:46 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://www.orafaq.com/forum/u/187446/

OP has not visited this forum since day (s)he started this thread

>Using above procedure we can run the query dynamically.
You can also poke yourself in the eye with a sharp pencil.
But you are advised against doing either.
Re: ora-00900 invalid sql statement while using open cursor for variable [message #642177 is a reply to message #642175] Fri, 04 September 2015 09:04 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
He might be have some issues. What's your problem??? If u know then do it. Else leave it...
Re: ora-00900 invalid sql statement while using open cursor for variable [message #642184 is a reply to message #642177] Fri, 04 September 2015 10:07 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

"u"? but U is dead.



Previous Topic: Table Lock
Next Topic: Remove Second Occurrence of a word in String
Goto Forum:
  


Current Time: Wed Apr 24 19:33:36 CDT 2024