Solved

ORA-00920: invalid relational operator

Posted on 2009-07-06
5
946 Views
Last Modified: 2012-05-07



function getFormattedString(f_inputString varchar2,
                            f_param       varchar2) return varchar2 is
  v_result             varchar2(8000);
  v_cnt                number := 1;
  v_Array eact_reports.t_array;
begin

  v_Array := Eact_Reports.split(f_inputString, ',');

  WHILE v_cnt <= v_Array.COUNT LOOP
    v_result := v_result || '''' || v_Array(v_cnt) || '''' || ',';
    v_cnt    := v_cnt + 1;
  end loop;

  if v_result is not null then
    v_result := f_param || ' IN (' ||
                substr(v_result, 0, length(v_result) - 1) || ')';
  else
    v_result := f_param ||' like  ''%''';
  end if;
  return v_result;
end getFormattedString;

select *  from EMP
where
    EXAMPLE_PACKAGE.getFormattedString(A,B,C,EMP.CODE)


I have written above function in a package.
i am calling this function in the where condition;but i am expecting the query should be formatted like this;

select *  from table
where
EMP.CODE IN('A','B','C')

but i am getting  below expection

ORA-00920: invalid relational operator
0
Comment
Question by:chaitu chaitu
  • 2
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24786698
>where     EXAMPLE_PACKAGE.getFormattedString(A,B,C,EMP.CODE)
that won't work, as it will interprete like this:

WHERE ' EMP.CODE IN(''A'',''B'',''C'') '
aka, a single string value. it cannot know you want to evaluate that "string" as part of the sql itself.

you will need dynamic sql. however, with SELECT, this will be tricky.
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 24786754
actually we have written a view for this query.

how to write dynamic sql for this or any other solution?
0
 
LVL 47

Accepted Solution

by:
schwertner earned 500 total points
ID: 24787010
Functions deliver one value and could be written in SQL.

But

select *  from EMP
where
    EXAMPLE_PACKAGE.getFormattedString(A,B,C,EMP.CODE)

delivers ONE value let us say 10

Then

select *  from EMP
where
    10

has no meaning

Possible

select *  from EMP
where
    ammount < EXAMPLE_PACKAGE.getFormattedString(A,B,C,EMP.CODE)

is more meaningfull

Its is like

select *  from EMP
where
    ammount < 10




0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 24792502
function getFormattedString(f_inputString varchar2,f_delim varchar2) return varchar2
  is
  v_result varchar2(8000) ;
  v_cnt number:=1;
  v_Array eact_reports.t_array ;
  begin
   
             
                v_Array := Eact_Reports.split(f_inputString , f_delim);
   
                WHILE v_cnt <= v_Array.COUNT LOOP
                v_result := v_result || '''' || v_Array(v_cnt) || '''' || ',';
                v_cnt :=v_cnt+1;
                end loop;
     if v_result is not null then
                            v_result  := ''|| ' ('||substr(v_result ,1,length(v_result)-1 )||')';
                            else
                            v_result :=' LIKE ''%'' ';
                end if;        
  return v_result;
  end ;
END tax_report_package;

I have changed like this but still records r not coming.

select *  from table
where
AND EMP.CODE IN (SELECT EXAMPLE_PACKAGE.getFormattedString('A$B$C','$') FROM DUAL);
0
 
LVL 47

Expert Comment

by:schwertner
ID: 24792861
This is correct and it should formally work.
Investigate the values and their type.
The function returns one value of type varchar2.
What is the type of EMP.CODE?
If it is number use conversion TO_NUMBER or TO_CHAR.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
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 shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

809 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