Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Dynamically spescifying a select clause for a cursor

Re: Dynamically spescifying a select clause for a cursor

From: Peter Schneider <peter.schneider_at_okay.net>
Date: 1998/03/24
Message-ID: <3516f5df.18026407@news.okay.net>#1/1

On Mon, 23 Mar 1998 16:19:55 -0600, fkgrass_at_hotmail.com wrote:

>How do I dynamically specify the select for a cursor. Here is what I want to
>do. This is just an example of what i want to do.
>declare
>c_cursor cusor;
>l_sql long:= 'select * from dual';
>begin
> open c_cursor for l_sql;
> for em in c_cursor loop
> ....
> end loop;
>end;
>
>Any ideas?
>
>Thanks,
>Fred Grass

Hi Fred,

you can do it with dynamic SQL using the DBMS_SQL package. A simple example which retrieves all empnos and enames from Scott's well known emp table could look like this:

DECLARE
   hndl INTEGER; -- the cursor handle    exec INTEGER; -- dummy return value for DBMS_SQL.EXECUTE    stmt VARCHAR2(32767); -- holds our statement

BEGIN

   END LOOP;

   EXCEPTION WHEN others THEN

      IF DBMS_SQL.IS_OPEN(hndl) THEN
         -- don't forget to close the cursor in case of error
         DBMS_SQL.CLOSE_CURSOR(hndl);
      END IF;
      RAISE;

END; You can find all details about this powerful package in the Oracle developer docs; a short but quite good overview is given in the package spec of DBMS_SQL itself.

HTH
Peter

-- 
Peter Schneider
peter.schneider_at_okay.net
Received on Tue Mar 24 1998 - 00:00:00 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US