Handling Oracle SQL in over 1000 solutions

Source: Internet
Author: User

Handle the Oracle SQL statement in clause (where ID in (1, 2, ..., 1000, 1001)), and if more than 1000 items in the clause will be an error. This is primarily a limitation that Oracle considers performance issues. If you want to solve the minor problem, you can use the where ID (1, 2, ..., +) or ID (1001, ...)

 PackageWindy.learn;Importjava.util.Collection;Importorg.apache.commons.lang3.StringUtils; Public classOraclesqlutils {/*** x, y divide, rounding up *@paramx *@paramy *@returndivide x, y, rounding up*/    Private Static intCeildiv (intXinty) {intR = x/y; returnR * y = = x? R:r + 1; }     Public StaticString Getoraclesqlin (collection<string>IDs, String field) {        returnGetoraclesqlin (IDs, 1000, field); }    /*** An error is found in the Oracle SQL statement in clause if more than 1000 items are in the clause. This is primarily a limitation that Oracle considers performance issues.     If you want to solve the minor problem, you can use the * where ID (1, 2, ..., +) or ID (1001, ...) *      * @paramCollection Object in the IDS * in statement *@paramnumber of conditions that appear in the count * in statement *@paramdatabase query fields corresponding to field * in statements *@returnreturns the field in (...) or field in (...) string*/     Public StaticString Getoraclesqlin (collection<string> IDs,intcount, String field) {string[] Idsarr= Ids.toarray (NewString[0]); Count= Math.min (count, 1000); intLen =idsarr.length; intSize =Ceildiv (Len, Count); StringBuilder Builder=NewStringBuilder ();  for(inti = 0; i < size; i++) {            intFromIndex = i *count; intToindex = math.min (FromIndex +count, Len); String productId=Stringutils.defaultifempty (Stringutils.join (Idsarr,"', '", FromIndex, Toindex), ""); if(I! = 0) {builder.append ("or"); } builder.append (field). Append ("In ('"). Append (ProductId). Append ("‘)"); }        returnStringutils.defaultifempty (builder.tostring (), Field+ "in (')"); }}

Reference: http://www.cnblogs.com/hoojo/archive/2012/08/31/2665396.html

Handling Oracle SQL in over 1000 solutions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.