解析自引用表时 CTE 中的无限循环 [英] Infinite loop in CTE when parsing self-referencing table

查看:70
本文介绍了解析自引用表时 CTE 中的无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下通用表表达式来解析自引用表.但是CTE不起作用,产生无限循环并产生错误:

I'm using the following Common Table Expression to parse self-referencing table. But the CTE does not work, produces and infinite loop and generates an error:

Msg 530, Level 16, State 1, Line 1 语句终止.这在语句完成之前,最大递归 100 已用完.

Msg 530, Level 16, State 1, Line 1 The statement terminated. The maximum recursion 100 has been exhausted before statement completion.

如何修改此 CTE 以使其正常工作?

How to modify this CTE to get it work?

SET NOCOUNT ON;
USE tempdb;

IF OBJECT_ID('dbo.Employees', 'U') IS NOT NULL DROP TABLE dbo.Employees;

CREATE TABLE dbo.Employees
(
  empid   INT         NOT NULL PRIMARY KEY,
  mgrid   INT         NULL     REFERENCES dbo.Employees,
  empname VARCHAR(25) NOT NULL,
  salary  MONEY       NOT NULL,
  CHECK (empid > 0)
);

INSERT INTO dbo.Employees(empid, mgrid, empname, salary) VALUES
  (1,  1, 'David'  , $10000.00),
  (2,  1,    'Eitan'  ,  $7000.00),
  (3,  1,    'Ina'    ,  $7500.00),
  (4,  2,    'Seraph' ,  $5000.00),
  (5,  2,    'Jiru'   ,  $5500.00),
  (6,  2,    'Steve'  ,  $4500.00),
  (7,  3,    'Aaron'  ,  $5000.00),
  (8,  5,    'Lilach' ,  $3500.00),
  (9,  7,    'Rita'   ,  $3000.00),
  (10, 5,    'Sean'   ,  $3000.00),
  (11, 7,    'Gabriel',  $3000.00),
  (12, 9,    'Emilia' ,  $2000.00),
  (13, 9,    'Michael',  $2000.00),
  (14, 9,    'Didi'   ,  $1500.00);

; with  Tree as
        (
        SELECT  empid
        ,       mgrid
        ,       1 as lv
        ,       1 as level1
        ,       null as level2
        ,       null as level3
        ,       null as level4
        ,       null as level5
        FROM    Employees
        WHERE   empid = 1 and mgrid = 1
        UNION ALL
        SELECT  E.empid
        ,       E.mgrid
        ,       T.lv + 1
        ,       T.level1
        ,       case when T.lv = 1 then E.empid else t.level2 end
        ,       case when T.lv = 2 then E.empid else t.level3 end
        ,       case when T.lv = 3 then E.empid else t.level4 end
        ,       case when T.lv = 4 then E.empid else t.level5 end
        FROM    Employees AS E
        JOIN    Tree T
        ON      E.mgrid = T.empid
        )
select  *
from Tree
order by empid

首选输出是

+-------+-------+----+--------+--------+--------+--------+--------+
| empid | mgrid | lv | level1 | level2 | level3 | level4 | level5 |
+-------+-------+----+--------+--------+--------+--------+--------+
|     1 |     1 |  1 |      1 | NULL   | NULL   | NULL   | NULL   |
|     2 |     1 |  2 |      1 | 2      | NULL   | NULL   | NULL   |
|     3 |     1 |  2 |      1 | 3      | NULL   | NULL   | NULL   |
|     4 |     2 |  3 |      1 | 2      | 4      | NULL   | NULL   |
|     5 |     2 |  3 |      1 | 2      | 5      | NULL   | NULL   |
|     6 |     2 |  3 |      1 | 2      | 6      | NULL   | NULL   |
|     7 |     3 |  3 |      1 | 3      | 7      | NULL   | NULL   |
|     8 |     5 |  4 |      1 | 2      | 5      | 8      | NULL   |
|     9 |     7 |  4 |      1 | 3      | 7      | 9      | NULL   |
|    10 |     5 |  4 |      1 | 2      | 5      | 10     | NULL   |
|    11 |     7 |  4 |      1 | 3      | 7      | 11     | NULL   |
|    12 |     9 |  5 |      1 | 3      | 7      | 9      | 12     |
|    13 |     9 |  5 |      1 | 3      | 7      | 9      | 13     |
|    14 |     9 |  5 |      1 | 3      | 7      | 9      | 14     |
+-------+-------+----+--------+--------+--------+--------+--------+

推荐答案

无限循环的原因是 empid=mgrid 的第一条记录.为了处理这个问题,你应该包含一个累积字段(在这个例子中为 levels)来存储你已经处理过的 mgrid 并检查 emid 是否已经存在在这个列表中以避免循环.

The reason of an infinite loop is the first record where empid=mgrid. To handle this issue you should include a cumulative field (levels in this example) to store mgrid you have already processed and check if emid is already in this list to avoid a loop.

这是一个查询:

with Tree as
        (
        SELECT  empid
        ,       mgrid
        ,       1 as lv
        ,       1 as level1
        ,       null as level2
        ,       null as level3
        ,       null as level4
        ,       null as level5
        ,       cast(mgrid as varchar(max)) levels  
        FROM    Employees
        WHERE   empid = 1 and mgrid = 1
        UNION ALL
        SELECT  E.empid
        ,       E.mgrid
        ,       T.lv + 1
        ,       T.level1
        ,       case when T.lv = 1 then E.empid else t.level2 end
        ,       case when T.lv = 2 then E.empid else t.level3 end
        ,       case when T.lv = 3 then E.empid else t.level4 end
        ,       case when T.lv = 4 then E.empid else t.level5 end
        ,       T.levels+','+cast(E.mgrid as varchar(max)) levels   

          FROM    Employees AS E
        JOIN    Tree T
        ON      E.mgrid = T.empid 
                and (','+T.levels+',' 
                      not like 
                     '%,'+cast(E.empid as varchar(max))+',%')
        )
select  *
from Tree
order by empid

这里是 SQLFiddle 演示

这篇关于解析自引用表时 CTE 中的无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆