Solved

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

Posted on 2008-10-16
3
6,938 Views
Last Modified: 2013-12-07
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.
0
Comment
Question by:bhavanisharansingh
  • 3
3 Comments
 
LVL 27

Expert Comment

by:sujith80
ID: 22728871
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

0
 
LVL 27

Expert Comment

by:sujith80
ID: 22728882
missed out the bind variable. the changed code will look like this.
0
 
LVL 27

Accepted Solution

by:
sujith80 earned 500 total points
ID: 22728890

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 ;        
       -- you should open the ref cur here
       open p_i_result for sql_str 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;

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
exp/imp 25 101
Row_number in SQL 6 45
running myfile.SQL from command line SQLPLUS 12c does not exit. 7 37
Oracle Query to Find number of process consumed by a session 15 54
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question