cte没有正确显示parentid

时间:2013-12-27 14:35:42

标签: common-table-expression sql-server-2012-express localdb

分贝:

menuid  Name                          Url                                   Parentid  Value
1   Products                      node                                      NULL    0
2   Windows                       node                                      NULL    0
3   Store                         node                                      NULL    0
4   Download                      node                                      NULL    0
5   About                         node                                      NULL    0
6   Office                        node                                      1   0
7   Office 2003                   #                                         6   1
8   Office 2007                   #                                         6   1
9   Office 2010                   #                                         6   1
10  Mobile                        node                                      1   0
11  IPhone                        #                                         10  1
12  Htc                           #                                         10  1
13  XP                            #                                         2   1
14  Vista                         #                                         2   1
15  Windows 7                     #                                         2   1
16  Chrome                        node                                      4   0

查询:

"WITH temp (menuid, name, url, parentid,value ) AS "+
                " (  SELECT  m.menuid, m.name, m.url, m.parentid,m.value " +
                " FROM menutable m "+
                " where m.menuid=1 " +

                " UNION ALL "+

                " SELECT mt.menuid, mt.name, mt.url, mt.parentid,mt.value " +
                " FROM  menutable mt INNER JOIN  temp t " +
                " on mt.parentid=t.menuid "+
                ") "+
                "SELECT menuid, name, url, COALESCE(parentid,0),value "+
                "FROM temp "

结果:

MenuID  Name           ParentID Url  value
1   Products    0   node    0
6   Office          0   node    0
10  Mobile          0   node    0
11  IPhone          0   #   1
12  Htc         0   #   1
7   Office 2003 0   #   1
8   Office 2007 0   #   1
9   Office 2010 0   #   1

为什么parentid总是0?我检查其他例子,但无法找到错误。谢谢... ................................. ................................ ............................... ...............................

0 个答案:

没有答案