分批拼接SQL IN 查询需要的ID字段值,解决IN 查询的ID过多问题

SQL查询使用的IN条件字段很多的时候,会造成SQL语句很长,大概SQL语句不能超过8K个字符,也有说IN不能超过1000个条件,总之太长了不行,需要拆分条件分批处理。下面提供一个将Int类型的条件字段值进行字符串拼接的方法。看代码:

复制代码
class Program
    {
        static void Main(string[] args)
        {
            int[] source = { 1,2,3,4,5,6,7,8,9,10};
            ShowSource(source);

            List<string> list = BatchJoinArray2String(source, 5).ToList();
            ShowList(list);

            List<string> list2 = BatchJoinArray2String(source, 3).ToList();
            ShowList(list2);

            Console.Read();
        }

        static void ShowSource(int[] source)
        {
            string sourceStr = string.Join(",", source);
            Console.WriteLine("Source Arrar:{0}", sourceStr);
        }

        static void ShowList(List<string> lst)
        {
            foreach (string item in lst)
                Console.WriteLine("\""+item+"\"");
            Console.WriteLine("--------------------");
        }

        static IEnumerable<string> BatchJoinArray2String(int[] arrSource,int batchSize)
        {
            if (batchSize <= 1) throw new ArgumentOutOfRangeException("batchSize 批处理大小不能小于1");
           
            if (arrSource.Length > batchSize)
            {
                int[] arr10 = new int[batchSize];
                int j = 0;
                for (int i = 0; i < arrSource.Length; i++)
                {
                    if (j < batchSize)
                    {
                        arr10[j++] = arrSource[i];
                    }
                    else
                    {
                        j = 0;
                        string str = string.Join(",", arr10);
                        arr10[j++] = arrSource[i];
                        yield return str;

                    }
                }
                if (j > 0) //还有剩余
                {
                    int[] arr0 = new int[j];
                    Array.Copy(arr10, arr0, j);
                    string str = string.Join(",", arr0);
                    yield return str;
                }
            }
            else
            {
                string str = string.Join(",", arrSource);
                yield return str;
            }
        }
    }
复制代码

运行这个示例程序,得到下面输出:

复制代码
Source Arrar:1,2,3,4,5,6,7,8,9,10
"1,2,3,4,5"
"6,7,8,9,10"
--------------------
"1,2,3"
"4,5,6"
"7,8,9"
"10"
--------------------
复制代码

 

在你的程序中,可以像下面这样使用:

复制代码
 string sql_update=@"
update t2 set 
  AA  =1 ,
  BB ='2222'
FROM [MyTable] as t2 
WHERE t2.[ID] in  ( @IDs );
";
//每次更新50条记录
            using (SqlConnection conn = new SqlConnection(DefaultConnectionString))
            {
                conn.Open();
                foreach (string ids in BatchJoinArray2String(XXXIds.ToArray(), 50))
                {
                    string sql = sql_update.Replace("@IDs", ids);
                    SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);
                }
              
                conn.Close();
            }
复制代码

该功能将集成在SOD框架中,敬请期待。

 

posted on   深蓝医生  阅读(1182)  评论(2编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2014-03-17 .NET DLR 上的IronScheme 语言互操作&&IronScheme控制台输入中文的问题

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示