Solved

ORA-00920: invalid relational operator

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

Suggested Solutions

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…
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

929 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

12 Experts available now in Live!

Get 1:1 Help Now