SQL Server 2008 R2:递归查询

时间:2015-10-06 04:43:03

标签: sql-server sql-server-2008-r2

这是以下问题:Prepare a recursive query

我有两个列的表,即cola和colb,如下所示:

表:测试

create table Test
(
 cola int,
 colb int
);

我输入的记录是:

Cola   Colb
------------
1      2
1      3
1      4
2      5
2      6
2      3
3      2
3      4
3      7
3      10
10     11
11      12
11     13
11     14
12     15
13     16 
14     99
15     88
16     77

注意:现在我想显示与我已通过的值相关联的唯一记录。例如,如果我将值传递为1,那么它应该向我显示连接的数字,并形成像树一样的连接。

对于上述要求,我从Dark Knight获得了如下所示的脚本,效果很好。

;WITH CTE AS
(
   SELECT COLA,COLB,','+CAST(COLA AS VARCHAR(MAX))+',' AS CHCK FROM test WHERE COLA=1
   UNION ALL
   SELECT C1.COLA,C1.COLB,C.CHCK+CAST(C1.cola AS VARCHAR(MAX))+','
   FROM CTE C INNER JOIN test C1 ON  C.colb = C1.cola
   WHERE CHARINDEX(','+CAST(C.colb AS VARCHAR(MAX))+',',C.CHCK)=0
),
OUTERCTE AS
(   
    SELECT DISTINCT COLA,COLB,ROW_NUMBER() OVER(PARTITION BY Colb ORDER BY Cola) rn FROM CTE --ORDER BY COLA
)
SELECT Cola,Colb FROM OUTERCTE
WHERE rn<=1
ORDER BY CASE WHEN Cola = 1 THEN 1 ELSE 2 END;

这给了我这个:

----------------
Cola        Colb
----------------
1           2   
1           3   
1           4   
2           5   
2           6   
3           7   
3           10  
10          11  
11          12  
11          13  
11          14  
12          15  
13          16  
16          77  
15          88  
14          99  

要求:现在我想显示记录的级别。

预期结果

    ------------------------------
    Cola        Colb        Level
    ------------------------------
    1           2           1
    1           3           1
    1           4           1
    2           5           2
    2           6           2
    3           7           2
    3           10          2
    10          11          3
    11          12          4
    11          13          4
    11          14          4
    12          15          5   
    13          16          5
    16          77          6
    15          88          6
    14          99          5

1 个答案:

答案 0 :(得分:2)

private void textBox1_KeyPress( object sender, KeyPressEventArgs e )
{ 
   if( !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) )  
      e.Handled = true;
}