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

Home -> Community -> Usenet -> c.d.o.server -> Re: Dynamic SQL and Cursors

Re: Dynamic SQL and Cursors

From: andrewst <member14183_at_dbforums.com>
Date: Thu, 21 Aug 2003 09:07:12 -0400
Message-ID: <3269312.1061471232@dbforums.com>

Originally posted by Jamie Jones

> Hi,

>

> I am having a few problems trying to execute dynamically generated
> SQL in an

> Oracle (version 9i if it is relevant) function. The simplified
> version of

> the function is as follows:

>

> -------------------------------------

> CREATE OR REPLACE FUNCTION GetFilterPopulation

> ( PRM_FilterID_Num IN NUMBER DEFAULT -1,

> PRM_UserID_Num IN NUMBER DEFAULT -1)

> RETURN NULL

> AS

> TYPE GenericCurTyp IS REF CURSOR;

> cur GenericCurTyp;

> SelectClause_Txt varchar2(1000);

> BEGIN

>

> SelectClause_Txt := 'SELECT UserID FROM Users'

>

> -- Execute the select and return appropriate data

> OPEN cur FOR ':s' USING SelectClause_Txt;

> LOOP

> DBMS_OUTPUT.PUT_LINE (cur.UserID);

> END LOOP;

> END;

> /

> -------------------------------------

>

> The error message that I am recieveing is "PLS-00487: Invalid
> reference to

> variable 'CUR'" which relates to this line:

> DBMS_OUTPUT.PUT_LINE (cur.UserID);

>

> Any help with where I am going wrong here would be greatly
> appreciated.

>

> Thanks in advance.

> Jamie

The correct code for the OPEN is:

  OPEN cur FOR SelectClause_Txt;

The USING clause is for assigning values to bind variables, not for assigning the entire SQL statement. For example:

OPEN cur FOR 'select ename from emp where empno=:x' USING 1234;

This assigns the value 1234 to the bind variable :x used in the SQL statement.

--
Posted via http://dbforums.com
Received on Thu Aug 21 2003 - 08:07:12 CDT

Original text of this message

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