SQL Server Equivalent of Oracle 'CONNECT BY PRIOR', and 'ORDER SIBLINGS BY'

前端 未结 3 1918
离开以前
离开以前 2020-12-09 00:05

I\'ve got this Oracle code structure I\'m trying to convert to SQL Server 2008 (Note: I have used generic names, enclosed column names and tabl

3条回答
  •  情书的邮戳
    2020-12-09 00:40

    The option given by the user "a_horse_with_no_name" worked for me. I changed the code and applied it to a menu generator query and it worked the first time. Here is the code:

    WITH tree(option_id,
           option_description,
          option_url,
          option_icon,
          option_level,
          sort_path)
         AS (
         SELECT ppo.option_id,
                ppo.option_description,
              ppo.option_url,
              ppo.option_icon,
              1 AS option_level,
              CAST('/' + RIGHT('00' + CONVERT(VARCHAR, ppo.option_index), 6) AS VARCHAR(MAX))
         FROM security.options_table_name ppo
         WHERE ppo.option_parent_id IS NULL
         UNION ALL
         SELECT co.option_id,
                co.option_description,
              co.option_url,
              co.option_icon,
              po.option_level + 1,
              po.sort_path + '/' + RIGHT('00' + CONVERT(VARCHAR, co.option_index), 6)
         FROM security.options_table_name co,
              tree AS po
         WHERE po.option_id = co.option_parent_id)
         SELECT *
         FROM tree
        ORDER BY sort_path;
    

提交回复
热议问题