如下的一个复合sql语句, 要获得一个不被()括起来的 where 
String sql= " select  a.*,b.* ,c.* , a.zl || a.hm as  zlhm, 
(select top 1  kindname from  codetable1 where t1.xh= a.xh and  t1.xh like '10%') as knamefrom  table1 a, table2 b , table3 c  where ( a.area = b.area  or a.area= c.area  ) and a.city = c.city "如上,如果有这样的where ,就 将这个where 之后的内容 前后加上括号和自己写的一段内容,即效果如下
... where  (   ( a.area = b.area  or a.area= c.area  ) and a.city = c.city     )  and  1=2 
然后返回这个被变动过的sql如果找不到这样的where 就返回"".
很实用的,在线欢迎拍砖
   

解决方案 »

  1.   

    其实不是很明白需求
    符合要求的where后的内容全都放在()内?这样的where能保证最多只有一个吗?
    如果找不到这样的where 就返回""又是什么意思?String sql = @" select  a.*,b.* ,c.* , a.zl || a.hm as  zlhm, 
    (select top 1  kindname from  codetable1 where t1.xh= a.xh and  t1.xh like '10%') as kname 
    from  table1 a, table2 b , table3 c  where ( a.area = b.area  or a.area= c.area  ) and a.city = c.city ";
    string customContent = "and  1=2";
    Regex reg = new Regex(@"(?is)(?<!\([^()]*)where(?![^()]*\))(.+)");
    string result = reg.Replace(sql, " where ( $1 ) " + customContent);
    richTextBox2.Text = result;
      

  2.   

    sql的语法, 同一层里where只能有一个的.实际应用只是为了迅速返回这个拼接的SQL是否正确.鸣谢过客兄和各位支持和帮助!
      

  3.   

    你是想利用正则 来改变sql   的语句是么?
      

  4.   

    用indexof找一下位置,用substring截取一下