• <bdo id='16U4n'></bdo><ul id='16U4n'></ul>

    <small id='16U4n'></small><noframes id='16U4n'>

  • <legend id='16U4n'><style id='16U4n'><dir id='16U4n'><q id='16U4n'></q></dir></style></legend>

      1. <i id='16U4n'><tr id='16U4n'><dt id='16U4n'><q id='16U4n'><span id='16U4n'><b id='16U4n'><form id='16U4n'><ins id='16U4n'></ins><ul id='16U4n'></ul><sub id='16U4n'></sub></form><legend id='16U4n'></legend><bdo id='16U4n'><pre id='16U4n'><center id='16U4n'></center></pre></bdo></b><th id='16U4n'></th></span></q></dt></tr></i><div id='16U4n'><tfoot id='16U4n'></tfoot><dl id='16U4n'><fieldset id='16U4n'></fieldset></dl></div>
      2. <tfoot id='16U4n'></tfoot>

        带有数据循环的 SQL Server 2005 递归查询 - 可能吗?

        时间:2023-10-10
          <tbody id='7UVxo'></tbody>

        • <i id='7UVxo'><tr id='7UVxo'><dt id='7UVxo'><q id='7UVxo'><span id='7UVxo'><b id='7UVxo'><form id='7UVxo'><ins id='7UVxo'></ins><ul id='7UVxo'></ul><sub id='7UVxo'></sub></form><legend id='7UVxo'></legend><bdo id='7UVxo'><pre id='7UVxo'><center id='7UVxo'></center></pre></bdo></b><th id='7UVxo'></th></span></q></dt></tr></i><div id='7UVxo'><tfoot id='7UVxo'></tfoot><dl id='7UVxo'><fieldset id='7UVxo'></fieldset></dl></div>

          <small id='7UVxo'></small><noframes id='7UVxo'>

            <bdo id='7UVxo'></bdo><ul id='7UVxo'></ul>
            • <legend id='7UVxo'><style id='7UVxo'><dir id='7UVxo'><q id='7UVxo'></q></dir></style></legend>

                  <tfoot id='7UVxo'></tfoot>
                1. 本文介绍了带有数据循环的 SQL Server 2005 递归查询 - 可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个标准的老板/下属员工表.我需要选择一个老板(由 ID 指定)和他的所有下属(以及他们的下属等).不幸的是,现实世界的数据中有一些循环(例如,两个公司所有者都将对方设置为他们的老板).带有 CTE 的简单递归查询会因此而窒息(超过最大递归级别 100).员工还能被选上吗?我不在乎选择它们的顺序,只关心它们中的每一个都被选择了一次.<小时>补充:你想要我的查询?嗯......好吧......我虽然这很明显,但是 - 这是:

                  I've got a standard boss/subordinate employee table. I need to select a boss (specified by ID) and all his subordinates (and their subrodinates, etc). Unfortunately the real world data has some loops in it (for example, both company owners have each other set as their boss). The simple recursive query with a CTE chokes on this (maximum recursion level of 100 exceeded). Can the employees still be selected? I care not of the order in which they are selected, just that each of them is selected once.


                  Added: You want my query? Umm... OK... I though it is pretty obvious, but - here it is:

                  with
                  UserTbl as -- Selects an employee and his subordinates.
                  (
                      select a.[User_ID], a.[Manager_ID] from [User] a WHERE [User_ID] = @UserID
                      union all
                      select a.[User_ID], a.[Manager_ID] from [User] a join UserTbl b on (a.[Manager_ID]=b.[User_ID])
                  )
                  select * from UserTbl
                  

                  <小时>添加 2: 哦,如果不清楚 - 这是一个生产系统,我必须做一些升级(基本上添加一种报告).因此,如果可以避免,我宁愿不修改数据.


                  Added 2: Oh, in case it wasn't clear - this is a production system and I have to do a little upgrade (basically add a sort of report). Thus, I'd prefer not to modify the data if it can be avoided.

                  推荐答案

                  我知道这已经有一段时间了,但我认为我应该分享我在尝试每一个解决方案时的经验,这里是我的发现总结(可能是这篇文章?):

                  I know it has been a while but thought I should share my experience as I tried every single solution and here is a summary of my findings (an maybe this post?):

                  • 添加包含当前路径的列确实有效,但会影响性能,因此我不适合.
                  • 我找不到使用 CTE 的方法.
                  • 我编写了一个递归 SQL 函数,它将员工 ID 添加到表中.为了绕过循环引用,需要检查以确保没有重复的 ID 添加到表中.表现一般,但并不理想.

                  完成所有这些后,我想出了将 [合格] 员工的整个子集转储到代码 (C#) 并使用递归方法在那里过滤它们的想法.然后我将过滤后的员工列表写入数据表,并将其作为临时表导出到我的存储过程.令我难以置信的是,事实证明,这对于小型和相对较大的表(我尝试了多达 35,000 行的表)来说都是最快、最灵活的方法.

                  Having done all of that, I came up with the idea of dumping the whole subset of [eligible] employees to code (C#) and filter them there using a recursive method. Then I wrote the filtered list of employees to a datatable and export it to my stored procedure as a temp table. To my disbelief, this proved to be the fastest and most flexible method for both small and relatively large tables (I tried tables of up to 35,000 rows).

                  这篇关于带有数据循环的 SQL Server 2005 递归查询 - 可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQLite 循环语句? 下一篇:是否可以在 LINQ 中使用全文搜索 (FTS)?

                  相关文章

                  <small id='LM4dK'></small><noframes id='LM4dK'>

                    <legend id='LM4dK'><style id='LM4dK'><dir id='LM4dK'><q id='LM4dK'></q></dir></style></legend>
                      <bdo id='LM4dK'></bdo><ul id='LM4dK'></ul>
                    <tfoot id='LM4dK'></tfoot>

                      <i id='LM4dK'><tr id='LM4dK'><dt id='LM4dK'><q id='LM4dK'><span id='LM4dK'><b id='LM4dK'><form id='LM4dK'><ins id='LM4dK'></ins><ul id='LM4dK'></ul><sub id='LM4dK'></sub></form><legend id='LM4dK'></legend><bdo id='LM4dK'><pre id='LM4dK'><center id='LM4dK'></center></pre></bdo></b><th id='LM4dK'></th></span></q></dt></tr></i><div id='LM4dK'><tfoot id='LM4dK'></tfoot><dl id='LM4dK'><fieldset id='LM4dK'></fieldset></dl></div>