Solved

Limitation of IN Operator in Oracle 9i

Posted on 2004-04-23
5
3,893 Views
Last Modified: 2012-08-14
How many values can a IN operator contain in oracle 9i ..i mean if i have a query saying  emp_no in (100,200,300,400 etc etc)..what is the maximum number of values that the IN operator allows?Thanks for your time ..
0
Comment
Question by:Sri10
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:OMC2000
ID: 10899535
There is no direct limitation on the number of values in this list.
You can use IN with sub-select statement that may return any number of values:
IN (SELECT emp_no from employees)
The only real restriction is the max length of SQL statement - 4GB.
0
 
LVL 1

Author Comment

by:Sri10
ID: 10899580
i get this error when using a query from an asp page ..

Microsoft OLE DB Provider for ODBC Drivers error '80004005'
[Microsoft][ODBC driver for Oracle][Oracle]ORA-01795: maximum number of expressions in a list is 1000

and part of my query looks like this..

sqlWHERE = "synoRoadmap.prty_code in " & Chaine_ListeComboProdType
sqlWHERE = sqlWHERE & " AND synoRoadmap.prod_code in " & Chaine_ListeProdCode(here i get more than 1000 products)
sqlWHERE = sqlWHERE & " AND synoRoadmap.regi_code = '" & Chaine_RegiCode &"'"
sqlWHERE = sqlWHERE & " AND synoRoadmap.regi_code = region.regi_code "

what do you think about the error OMC2000?
0
 
LVL 15

Accepted Solution

by:
OMC2000 earned 125 total points
ID: 10899951
Oops, sorry, you are right, there is hard coded limitation on the number of expressions in the list. It is 1000.

The work around is getting values by a subquery. You could select all that keys from some table, even temporary one.
0
 
LVL 2

Expert Comment

by:n4nazim
ID: 10906396

Hi,

There is a work-around for this problem. I dont know how far this solution wld help u. here it is ...


I CREATED A TABLE NZTB1

create table nztb1 ( col1 number(10));

INSERTED 1000 RECORDS INTO IT.

declare
      n number(10);
begin
      for n in 1..1000
  loop
   insert into nztb1 values (n);
  end loop;
  commit;
end;  


CHECKED THE 1000 ROW COUNT

select count(1) from nztb1;


NOW IF I USE A NORMAL IN CLAUSE WITH MORE THAN 1000 ELEMENTS, IT GIVES THE SAME ERROR AS WHAT U GET ... NOTE AFTER THE LOOP I HAVE ADDED 1 MORE ELEMENT SO THAT THE ELEMENTS IN THE IN CLAUSE BECOME 1001

declare
 sqlstmt varchar2(13000);
 ctr number(10);
begin
      sqlstmt := '';
      for n in 1..1000
  loop
   sqlstmt := sqlstmt || n || ', ';
  end loop;      
  sqlstmt := sqlstmt || ' 0';
  sqlstmt := 'select count(1) from nztb1 where col1 in ( ' || sqlstmt || ' )';
  execute immediate sqlstmt into ctr;
  dbms_output.put_line(ctr);
end;  


NOW FOR THE WORK-AROUND. I DONT KNOW HOW FAR THIS WOULD WORK FOR U .. FOR ME IT WORKED BECAUSE THE LENGHT SQLSTMT IS LESS THAN 32767 CHARACTERS..

WHAT I AM DOING IS I PREPARED A SELECT INSIDE THE IN CLAUSE WITH UNION OF ALL VALUES ... FOR ME IT WORKED FINE. BUT I WLD ADVISE TO CHECK THE SPEED ISSUE OF THIS PROCEDURE ALSO BECUASE I AM USING AN UNION

declare
 sqlstmt varchar2(32767);
 ctr number(10);
begin
      sqlstmt := '';
      for n in 1..1000
  loop
   sqlstmt := sqlstmt || ' select ' || n || ' from dual ' || ' UNION ';
  end loop;      
  sqlstmt := sqlstmt || ' select 1 from dual ';
  sqlstmt := 'select count(1) from nztb1 where col1 in ( ' || sqlstmt || ' )';
  execute immediate sqlstmt into ctr;
 dbms_output.put_line(ctr);
end;  

Let me know the test resuilts

Rgds,
Nazim M
0
 
LVL 1

Author Comment

by:Sri10
ID: 11774271
the problem was solved long ago but was posting this now..we changed the sql so instead of expression we used subquery from temporary table..thanks OMC2000
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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL Query Syntax 6 86
Space Delimited Sql File 4 71
Oracle SQL Select Statement 19 59
PL/SQL Two statements 6 50
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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 set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

743 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

11 Experts available now in Live!

Get 1:1 Help Now