日期:2014-05-17  浏览次数:20717 次

开发时,oracle 的sql中常用union all 有没有什么不好的地方?请教下各位
经常碰到这种情况,前台传过来的参数是数组形式的,比如 机构代码 String[] sobids;职务代码String[]posts;
去后台查询的时候,机构和职务是一一对应的,例如: selcct ...  ... where 1=1 and sobid=sobids[i] and post=posts[i] ,但是前面要查询的字段是一样的,这个时候我,我不想在bs层循环的调用dao层,一般是把数组传过来,然后进行循环,用union all  对sql进行拼接,然后一次查出来。例如:
StringBuffer sql=new StringBuffer();
for (int i = 0; i < sobids.length; i++) {
    if(i!=0){
       sql.append(" union all ");
    }
    sql.append(" select dept_name,login_id,party_id,LS_ID from au_user ");
    sql.append(" where dept_flag='"+sobids[i]+"' and post='"+posts[i]+"' ");
}
这样的sql即使会显得比较长,但是格式化一下看起来也还好,想请教各位,这样的做法有什么弊端?会影响执行效率么?
ps:因为是在内网使用,并且有拦截器对非法字符进行了处理,所以sql一般不用担心sql注入的问题。

------解决方案--------------------
是,要用 or 和 and 拼装,但对于数据库来说只是一句SQL。

有索引情况下,就是N次索引定位查询。