Solved

ORA-00920: invalid relational operator

Posted on 2009-07-06
5
936 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.

Join & Write a Comment

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…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now