SQL使用IN参量不能超过1000的表现形式以及解决办法

 

如果出现这个错误说明你传的参量是超过了一千个值;列如,你拼接了1001个id;

如何解决那,我这里提供两种方法:

1.每1000条加一个or in

列:

原:select p.* from t_premium p where p.premium_id in ('0','1'........,'1000',......'2000');

现:select p.* from t_premium p where p.premium_id in ('0','1'......‘999’)or in (’1000',......'1999');

如何实现那?

看下边

 

 1     public static void main(String[] agrs) throws Exception {
 2         StringBuffer thirdPartyPayId=new StringBuffer();
 3         for (int j = 0; j < 20; j++) {
 4             if((j%10) == 0 && j > 1){
 5                 thirdPartyPayId.deleteCharAt(thirdPartyPayId.length() -1);
 6                 thirdPartyPayId.append(") OR " + "info.third_party_pay_id" + " IN ('" + j + "',");
 7             }else{
 8                 thirdPartyPayId.append("'").append(j).append("',");
 9             }
10         }
11         thirdPartyPayId.deleteCharAt(thirdPartyPayId.length() -1);
12         System.out.println(thirdPartyPayId.toString());
13     }

 

 我这里用j代替了循环的"info.third_party_pay_id" ,兄弟们用的时候可以遍历自己的list,获取参量;

thirdPartyPayId.deleteCharAt(thirdPartyPayId.length() -1):去掉多余的标点符号,不能省;

SELECT DISTINCT
               nvl(info.apply_policy_no,'无') applyPolicyNo
             FROM T_THIRD_PARTY_PAY_INFO info,t_department_define o
            WHERE info.department_code = o.internal_department_code(+)
            AND (info.third_party_pay_id IN ($thirdPartyPayId$ ))
            AND info.FLAG = '2'

注:1.这种方式效率很慢;数据量小的话也就无所谓;数据量大的话不建议使用;

  2.()这两个括号必须要,因为(+)这个不能和in, not in 一起使用,但是因为

2.传递的参量转换成一个sql查询

SELECT DISTINCT
               nvl(info.apply_policy_no,'无') applyPolicyNo
             FROM T_THIRD_PARTY_PAY_INFO info,t_department_define o
            WHERE info.department_code = o.internal_department_code(+)
            AND info.third_party_pay_id IN (

    SELECT
                          sd.third_party_pay_id
                     FROM T_SOURCE_DOCUMENT sd,t_voucher v
                    WHERE sd.VOUCHER_ID = v.VOUCHER_ID and v.voucher_code=#voucherCode#
                    AND sd.third_party_pay_id IS NOT NULL
                    and v.CANCEL_STATUS != '2'

       )
            AND info.FLAG = '2'

就是说你获取参量的那个查询sql放进来;

 

后记:应该还有join以及exists两种思路。但用起来感觉所锁定目标不太清晰。故没做考量;

 

posted @ 2018-08-14 19:23  ziChuangShi  阅读(6619)  评论(0编辑  收藏  举报