Ref cursor with Execute immediate

Naveen Chakravarthy picture Naveen Chakravarthy · Jun 23, 2011 · Viewed 22k times · Source

I want to get the results in ref_cursor, but I am not able to do that.

Please suggest me how to get the results in ref_cursor using Execute immediate

CREATE OR REPLACE PROCEDURE TEST_PROC_QT ( p_name IN VARCHAR2,
                                           p_result_set OUT sys_refcursor ) IS

  v_sql VARCHAR2(4000);

BEGIN

   v_sql := '';
   v_sql := 'SELECT * FROM USERS WHERE 1=1 ';

   IF p_name is not null THEN
     v_sql := v_sql || ' AND login_id = :v_name';
   ELSE
     v_sql := v_sql || ' AND ((1=1) or :v_name is null)';
   END IF;

   Dbms_output.put_line(v_sql);

   EXECUTE IMMEDIATE v_sql

   --OPEN p_result_set for v_sql
   --INTO p_result_set using p_name;  

END;

Answer

Tony Andrews picture Tony Andrews · Jun 23, 2011

Just:

OPEN p_result_set for v_sql using p_name;