Ref cursor with Execute immediate

时光怂恿深爱的人放手 提交于 2019-12-07 22:50:29

问题


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;

回答1:


Just:

OPEN p_result_set for v_sql using p_name;  



回答2:


  1. You need to create a type to keep the data you select is going to return,for example. Imagine this is yours users table user(id number, name varchar2), your type should look like:

    create type users_type as object (id number, name varchar2);
    
  2. Create a type table to keep object to created above.

    create type users_table_object as table of users_type;
    
  3. create the procedure:

    create the procedure test(
    cv_1  OUT SYS_REFCURSOR
    ....
    )
    as
    users_table_object;
    v_query VARCHAR2(2000);  
    
    begin
    v_query := N'select users_type(id, name) from users ';
    
    execute immediate
    bulk collect into users_table_object;
    
    open cv_1 for table(users_table_object)     
    


来源:https://stackoverflow.com/questions/6457085/ref-cursor-with-execute-immediate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!