将菜单和子菜单添加到数据库中

时间:2015-12-08 10:29:01

标签: sql sql-server-2005

我在下面有一个查询

SELECT DISTINCT a.mkey, 0 child_menu_mkey, UPPER(a.menu_name) parent_menu, '' child_menu 
FROM wms_menu_hdr a,
     wms_menu_hdr b 
WHERE a.mkey = b.parent_mkey 
UNION 
SELECT DISTINCT b.mkey, b.mkey child_menu_mkey, '' parent_menu, b.menu_name child_menu 
FROM wms_menu_hdr a,
     wms_menu_hdr b 
WHERE a.mkey = b.parent_mkey 
ORDER BY 1,2 

显示结果

我正在使用SQL-server-2005

2 个答案:

答案 0 :(得分:1)

在SQL Server 2005中,没有窗口函数,您需要某种子查询,例如:

C:\ Drive

答案 1 :(得分:1)

我的回答是另一个问题,但是我们清除了这个问题,所以试试这个:

SELECT parent_key, child_key, parent_menu, child_menu
FROM
(SELECT DISTINCT a.mkey AS mkey, 0 child_menu_mkey, UPPER(a.menu_name) parent_menu, '' child_menu, mkey AS parent_key 
        FROM wms_menu_hdr a
        WHERE a.mkey IN (SELECT parent_mkey FROM wms_menu_hdr)
        UNION ALL
        SELECT DISTINCT b.mkey, b.mkey child_menu_mkey, '' parent_menu, b.menu_name child_menu, a.mkey AS parent_key
        FROM wms_menu_hdr a,
             wms_menu_hdr b 
        WHERE a.mkey = b.parent_mkey)
ORDER BY parent_key, child_menu_key;