问题:


//oracle数据库报错 delete from DD_ORDER_DETAIL where ORDERID=in(0,1,2,3,4,5,6,7.....1001);
c#写了一个方法解决


/// <summary> /// 拼接sql /// </summary> /// <param name="str">主sql语句</param> /// <param name="ids">要拼接的in里面的参数</param> /// <param name="count">每次执行条数</param> /// <returns></returns> public static string get(string str, string[] ids) { if (ids.Length > 0) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < ids.Length; i++) { if ((i % 1000) == 0 && i > 0) { sb.Remove(sb.Length - 1, 1); sb.Append(") or " + str + "in(" + ids[i] + ","); } else { sb.Append(ids[i] + ","); } } sb.Remove(sb.Length - 1, 1); return str + "in(" + sb.ToString() + ")"; } return ""; }
调用如下


static void Main(string[] args) { string str = "delete from DD_ORDER_DETAIL where ORDERID ="; string[] s = { }; List<string> list = new List<string>(); for (int i = 0; i < 1001; i++) { list.Add(i.ToString()); } s = list.ToArray(); ; Console.WriteLine(s.Length); string ss = get(str, s); Console.ReadKey(); }
效果如下


第二种:


//ids要插入in中的数据,sqlstr原始sql,num多少条执行一次 public static string[] CreateSQL(string[] ids, string sqlStr, int num) { string[] sqls = null; StringBuilder sb = new StringBuilder(); sb.Append(sqlStr); List<string> lt = new List<string>(); for (int i = 0; i < ids.Length; i++) { if (i != 0 && i % num == 0) { string s1 = sb.ToString(); s1 = s1.Remove(s1.Length - 1) + ")"; lt.Add(s1); sb.Length = 0; sb.Append(sqlStr); sb.Append(ids[i] + ","); if (i == ids.Length - 1) { string s2 = sb.ToString(); s2 = s2.Remove(s2.Length - 1) + ")"; lt.Add(s2); } } else { sb.Append(ids[i] + ","); if (i == ids.Length - 1) { string s = sb.ToString(); s = s.Remove(s.Length - 1) + ")"; lt.Add(s); } } } sqls = lt.ToArray(); return sqls; }
调用


static void Main(string[] args) { List<string> list = new List<string>(); for (int i = 0; i < 550; i++) { list.Add(i.ToString()); } string[] sqls = CreateSQL(list.ToArray(), "select * from tb_spkc where id in(", 500); Console.ReadKey(); }
效果

本文介绍了一种解决Oracle数据库中批量删除操作时遇到的SQL冲突问题的方法,通过拼接主SQL语句和动态参数,实现了高效且避免冲突的删除逻辑。同时,提供了两种实现方式,一种适用于一次性插入大量数据的场景,另一种则适用于分批处理数据。通过实例代码演示了如何使用这些方法,并展示了最终的效果。
1070

被折叠的 条评论
为什么被折叠?



