Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2008-10-16
3
6,926 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get the parent node - XMLTYPE 9 98
Web Service from a stored procdure oracle 10 58
Create table from select - oracle 6 53
PL SQL Search Across Columns 4 52
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

860 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