sql查询的最大参数个数

发布于 2024-07-19 14:58:29 字数 1401 浏览 7 评论 0原文

我已经尝试使用 LINQ 一段时间了。 枚举集合并在代码中更改其某些属性的典型方法如下所示:

ATDataContext dc = new ATDataContext(Settings.connection_string);

int[] col = ListViewClass.getListViewSelectedPositionTags(listView);

try
{
    foreach (var item in col)
    {
        var ctx = (from r in dc.MailingLists
                   where r.ID == item
                   select r).Single();

        ctx.Excluded = 'Y';
        ctx.ExcludedComments = reason;
    }

    dc.SubmitChanges();
}

后来我得到了一个建议,通过...似乎更聪明的方式来做到这一点:

var ctx = from r in dc.MailingLists
    where col.Contains(r.ID)
    select r;

foreach (var item in ctx)
{
    item.Excluded = 'Y';
    item.ExcludedComments = reason;
}

dc.SubmitChanges();
            

它在很多层面上都是有意义的,我喜欢这个解决方案。 它比第一个更智能、更快。

我已经在生产环境中使用这个解决方案一段时间了。

几周后,当搜索应用程序日志文件并看到以下内容时,我感到惊讶的是:

“传入的表格数据流 (TDS) 远程过程调用 (RPC) 协议流不正确。此中提供了太多参数RCP 请求。最大值为 2100。”

LINQ to SQL 将 where col.Contains(r.ID) 转换为 IN 子句查找内容喜欢:
WHERE ID IN (@p1, @p1, @p2 … )

col 集合达到(在我的例子中)超过 2100 个元素,并且查询无法执行。 我对这个问题做了一些研究,最终得到的结果是:

“…sql查询中的最大参数数量是2100。还有更多的限制,比如整个查询字符串不能更长超过 8044 个字符。”

我非常喜欢第二种解决方案。 我对 SQL Server 的这些硬编码限制感到非常失望。

我错过了什么? 我可以做些什么才能使用 where col.Contains(r.ID) 版本?

I do experiment with LINQ since some time.
Typical method to enumerate through a collection and change some of its properties in my code would look like:

ATDataContext dc = new ATDataContext(Settings.connection_string);

int[] col = ListViewClass.getListViewSelectedPositionTags(listView);

try
{
    foreach (var item in col)
    {
        var ctx = (from r in dc.MailingLists
                   where r.ID == item
                   select r).Single();

        ctx.Excluded = 'Y';
        ctx.ExcludedComments = reason;
    }

    dc.SubmitChanges();
}

Later on I have got an advice to do this by... seems like much smarter way:

var ctx = from r in dc.MailingLists
    where col.Contains(r.ID)
    select r;

foreach (var item in ctx)
{
    item.Excluded = 'Y';
    item.ExcludedComments = reason;
}

dc.SubmitChanges();
            

Iit makes sense on so many levels and I love this solution. It’s smart and faster than the first one.

I have used this solution in a production environment for some time.

What was my surprise after few weeks when searching an application log files and see this:

"The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Too many parameters were provided in this RCP request. The maximum is 2100."

The LINQ to SQL converts where col.Contains(r.ID) to INclause looking something like:
WHERE ID IN (@p1, @p1, @p2 … )

The col collection reached (in my case) more than 2100 elements and the query failed to perform. I have done some research on the problem and what I ended up is:

“… Maximum number of parameters in the sql query is 2100. There is more limitations, like the fact that the whole query string cannot be longer than 8044 characters.”

I have loved the second solution so much. I am so disappointed with these hard-coded limitations of the SQL Server.

Did I miss something?
Is there anything I can do to be able to use the where col.Contains(r.ID) version?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁与争疯 2024-07-26 14:58:29

限制是硬编码的

  • 每个存储过程的参数
  • 每个用户 2,100 个参数-定义函数 2,100

我之前编写了一些代码,将 Contains 查询拆分为批次并合并结果... 请参阅此处了解更多信息

The limits are hard-coded:

  • Parameters per stored procedure 2,100
  • Parameters per user-defined function 2,100

I wrote some code before that split the Contains query into batches and combined the results... see here for more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文