Cursor For Loop [message #389605] |
Mon, 02 March 2009 22:41  |
|
Hi All,
I was just working on one example on cursor for loop, I have one SQL Statment which will be generated dynamically, I want to use that sql statement in Cursor For Loop, but i have declared that SQL Statement as Varchar2,How do i use it ?
Please have a look at the following code,
CREATE OR REPLACE PROCEDURE testtest (i_depts IN VARCHAR2)
IS
i NUMBER;
l_sql VARCHAR2 (2000);
BEGIN
l_sql := 'Select empno from emp where deptno in (' || i_depts || ')';
DBMS_OUTPUT.put_line (l_sql);
FOR i IN l_sql
LOOP
EXIT WHEN SQL%NOTFOUND;
DBMS_OUTPUT.put_line ('EmpNo' || i.empno);
END LOOP;
END;
It will obviously through error because 'l_sql' is varchar2 and not cursor.
This is just an example to check how to use a Varchar2 string as the cursor.
Regards,
Ashoka BL
Bengaluru
|
|
|
|
Re: Cursor For Loop [message #389623 is a reply to message #389605] |
Mon, 02 March 2009 23:09   |
|
Adding to my requirement, i dont want it to be implemented with the RefCursor
Please find the DDL And DML below,
[Edit MC: Oracle code removed, don't post copyrighted material. (Standard EMP table is used in this topic.)]
The input Parameter will be passed to the Procedure as following,
Quote: |
SQL> execute testtest('20,30,40');
|
Regards,
Ashoka BL
[Updated on: Mon, 02 March 2009 23:14] by Moderator Report message to a moderator
|
|
|
Re: Cursor For Loop [message #389624 is a reply to message #389605] |
Mon, 02 March 2009 23:10   |
 |
Michel Cadot
Messages: 68765 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
A direct answer to your question is use "open mycursor for l_sql" (after declaring mycursor, of course).
But this is not the correct (most efficient) way to achieve your goal. Search as BlackSwan said.
Regards
Michel
[Updated on: Mon, 02 March 2009 23:15] Report message to a moderator
|
|
|
|
Re: Cursor For Loop [message #389627 is a reply to message #389605] |
Mon, 02 March 2009 23:27   |
|
Hi,
Thanks for the replies,
Quote: |
SQL is "compiled" as a static statement; when procedure is compiled.
SQL can not vary at run time
|
How will it be compiled or effected if i use REF CURSOR ?
But i believe we can achieve this by using REF CURSOR as,
Open RefCursor for l_sql;
Regards,
Ashoka BL
Bengaluru
|
|
|
|
Re: Cursor For Loop [message #389635 is a reply to message #389605] |
Tue, 03 March 2009 00:24   |
|
I always wanted to be polite and respect the people out here, but i dont know why does the other person be like that !!
I asked for the solution or wanted to share whatever i have,
Quote: |
Then why are you here?
Either you can or you can not.
|
This cannot be an answer, i think its not really required here to talk about anything too much.
If you want to share things or motivate people in doing that it is good.
I want to close this topic here.
This may be useful for everybody when they post here !!

Regards,
Ashoka BL
Bengaluru
-
Attachment: Respect.PNG
(Size: 8.27KB, Downloaded 965 times)
|
|
|
|
|