Quantcast
Channel: Way 2 Resources - General
Viewing all articles
Browse latest Browse all 10

How to Implement IN Operator for more than 1000 values in SQL or Oralce

$
0
0

Hi,

How to Implement IN Operator for more than 1000 values in SQL or Oralce
Select statement is used to get data from database and The IN operator allows us to specify multiple values in a WHERE clause in any select statement.
Like : SELECT column_name1, column_name2,..
FROM table_name
WHERE column_name1 IN (value1,value2,...)
But do you know how many values a IN statement can have or the maximum limit of values we can give in a IN statement? We can not give more than 1000 values in a IN operator while selecting data.If you do so you will get an error.
Now taking an example If my requirement is to fill a dictionary in Asp.Net with data filtered by more then 1000 of records and i have to add more then 1000 values in a IN operator with where clause. Then there are some ways to do so
1. Using OR Operator: By using OR Operator with IN operator in where clause with Select statement we can achieve our desired output here is the example:
Ex : SELECT column_name1, column_name2,..
FROM table_name
WHERE column_name1 IN (value1,value2,...value1000)
or column_name1 IN (value1001,value1002,...value2000)  and so on
2. Using Union Operator: By using Union operator with IN Operator in where clause with select statement we can achieve our desired output here is the example:
Ex : SELECT column_name1, column_name2,..
FROM table_name
WHERE column_name1IN (value1,value2,...value1000)
union
SELECT column_name1, column_name2,..
FROM table_name
WHERE column_name1IN (value1001,value1002,...value2000) and so on
3. Handle this situation in Asp.Net itself: We can handle this situation in Asp.Net data access page also (By sending queries in chunk). For doiong so we will make array list which contains approx 900 values as a string and then sending them one by one to database and getting data from database and add them to dictionary in asp.net. It will reduce the memory usage because it brings data in memory for a limited records only. For this situation we have to interact database again and again. It is likely to bind dictionary with implementation of IN operator for more then 1000 rows of data in Asp.Net.
Ex :  ArrayList arrLst = new ArrayList();
           Array ValuesList          // contains all the values
            double countRecords=0;
            Int32 stringCount=0;
            try
            {
                //count
                for (int i = 0; i < ValuesList.Count; i++)
                {
                    if (stringCount <= 900)
                    {
                        ValueStr += "'" + Convert.ToString(ValuesList[i].ColumnValue) + "',";
                       
                    }

                    if (stringCount == 900)
                    {
                        ValueStr = ValueStr.TrimEnd(',');                      
                        arrLst.Add(ValueStr);                     
                        ValueStr = string.Empty;
                      
                    }
                    if (stringCount < 900)
                        stringCount++;
                    else if (stringCount == 900)
                        stringCount = 0;

                }
                if (ValueStr != string.Empty)
                {
                    arrLst.Add(ValueStr.TrimEnd(',')); // Making Array List of values 900 in each element
                    ValueStr = string.Empty;                  
                }

                for (int i = 0; i < arrLst.Count; i++)// Making Queries and sending to database
                {
                    query = new StringBuilder();
                    query.Append(" select Column1,Column2,Column3");
                    query.Append(" from Table_Demo");
                    query.Append(" where (Column1 in (" + arrLst[i] + " ))");                  
                    query.Append(" order by Column1,Column2");

                    dbCommand = dataConnection.GetSqlStringCommand(query.ToString());
                    ValDataReader = dbConn.GetDataReader(dbCommand); // getting values in a DataReader

                    while (ValDataReader.Read())
                    {
                        ValDataDTO = new ValDataDTO();
            if (ValDataReader ["Column1"] != DBNull.Value)
                            Column1 = Convert.ToDouble(ValDataReader["Column1"]);
                        if (ValDataReader ["Column2"] != DBNull.Value)
                            ValDataDTO.Column2 = Convert.ToDouble(ValDataReader["Column2"]);
                        if (ValDataReader ["Column3"] != DBNull.Value)
                            ValDataDTO.Column3 = Convert.ToInt16(ValDataReader["Column3"]);
                      
                        keyforDict = Column1; // Taking Column1 as Key of dictionary
                        dictionaryOfValueData.Add(keyforDict, ValDataDTO);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
             
            }

 

Now we have 3 Approaches to Implement IN Operator for more than 1000 values in SQL or Oralce.

All the Best...

 


Viewing all articles
Browse latest Browse all 10

Trending Articles