Link to home
Start Free TrialLog in
Avatar of bhavanisharansingh
bhavanisharansinghFlag for India

asked on

Ref cursor or PLSQL table in the INTO clause of EXECUTE IMMEDIATE

Hi,
I have created a procedure that searches tables in the database.
Table name is decided at run time and i want the output of the query to be in REF CURSOR or PLSQL table to increase the performance.

Please find below the proc
Procedure pr_search_acronym (p_i_acronym varchar2, p_i_category integer DEFAULT NULL, p_i_user varchar2, p_i_result OUT acronym_record,p_i_error OUT varchar2,p_i_exit_status OUT integer )
IS
acronym_start_alphabet char(1);
acronym_table_name char(15);
acronym_result acronym_record;
sql_str varchar2(1000);
error_id number;
error_message varchar2(300);
p_i_error varchar2(500);
BEGIN
       /* Finding the table name, in which data is to be inserted*/
         acronym_start_alphabet := SUBSTR(p_i_acronym,1,1);
         acronym_table_name := 'TBLA_ACRONYM_' || acronym_start_alphabet;
      
       sql_str := 'SELECT * FROM ' || acronym_table_name
                          || ' WHERE ACRONYM LIKE :1 ' ;
                  
       EXECUTE IMMEDIATE sql_str INTO p_i_result USING p_i_acronym ;       
             
       EXCEPTION
                      WHEN OTHERS THEN
        p_i_error := SQLERRM;
         p_i_exit_status := 1;
           SELECT SEQ_EXCEPTION.NEXTVAL INTO error_id FROM DUAL;
           INSERT INTO TBLA_EXCEPTION (ERROR_ID, ERROR_DESC, ERROR_LOCATION,         USER_ID, ERROR_DATE)                                 VALUES ( error_id,p_i_error ,'SearchAcronym' ,p_i_user,sysdate );       
END pr_search_acronym;

Also note : TYPE acronym_record IS REF CURSOR;
But when i am trying to run the proc.
It gives following error
ORA-00932: inconsistent datatypes: expected CURSER got NUMBER

Please help me.
Thanks.
Avatar of Sujith
Sujith
Flag of United Kingdom of Great Britain and Northern Ireland image

Change the below line.

You cannot execute immediate into a ref cur. you have to open it using the sql stmt.
.
.
 
       --EXECUTE IMMEDIATE sql_str INTO p_i_result USING p_i_acronym ;        
       -- you should open the ref cur here
       open p_i_result for sql_str;
.
.

Open in new window

missed out the bind variable. the changed code will look like this.
ASKER CERTIFIED SOLUTION
Avatar of Sujith
Sujith
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial