SQL Server等效于Oracle' CONNECT BY PRIOR'和' ORDER SIBLINGS BY'

时间:2015-08-05 08:55:24

标签: sql-server oracle recursive-query siblings connect-by

我有这个Oracle代码结构我试图转换为SQL Server 2008 注意:我使用了通用名称,方括号' []'中包含的列名和表名,并进行了一些格式化以使代码更具可读性

SELECT [col#1], [col#2], [col#3], ..., [col#n], [LEVEL] 
FROM (SELECT [col#1], [col#2], [col#3], ..., [col#n] 
      FROM [TABLE_1] 
      WHERE ... ) 
CONNECT BY PRIOR [col#1] = [col#2] 
START WITH [col#2] IS NULL 
ORDER SIBLINGS BY [col#3]

上述代码的SQL Server等效模板是什么?

具体来说,我正在努力解决 LEVEL 和' ORDER SIBLINGS BY' Oracle构造。

注意: 以上"代码"是一组Oracle过程的最终输出。基本上, ' WHERE'子句是动态构建的 ,并根据传递的各种参数进行更改。 代码块以' CONNECT BY PRIOR'开头。是硬编码的

供参考:

Simulation of CONNECT BY PRIOR of ORACLE in SQL SERVER文章已接近尾声,但未解释如何处理“{3}”文章。以及' ORDER SIBLINGS'结构体。 ......我的思绪正在转变!

SELECT name 
  FROM emp
  START WITH name = 'Joan'
  CONNECT BY PRIOR empid = mgrid

等同于:

WITH n(empid, name) AS 
   (SELECT empid, name 
    FROM emp
    WHERE name = 'Joan'
        UNION ALL
    SELECT nplus1.empid, nplus1.name 
    FROM emp as nplus1, n
    WHERE n.empid = nplus1.mgrid)
SELECT name FROM n

如果我有一个可以使用的初始模板,那么帮助我构建SQL Server存储过程以构建正确的T-SQL语句将会有很长的路要走。

非常感谢协助。

3 个答案:

答案 0 :(得分:11)

模拟LEVEL列

通过递增递归部分中的计数器,可以轻松地模拟级别列:

WITH tree (empid, name, level) AS  (
  SELECT empid, name, 1 as level
  FROM emp
  WHERE name = 'Joan'

  UNION ALL

  SELECT child.empid, child.name, parent.level + 1
  FROM emp as child
    JOIN tree parent on parent.empid = child.mgrid
)
SELECT name 
FROM tree;

模拟order siblings by

模拟order siblings by有点复杂。假设我们有一个列sort_order来定义每个父元素的顺序(不是整体排序顺序 - 因为那时order siblings不是必需的)那么我们可以创建一个列,它给我们一个整体排序顺序:

WITH tree (empid, name, level, sort_path) AS  (
  SELECT empid, name, 1 as level, 
         cast('/' + right('000000' + CONVERT(varchar, sort_order), 6) as varchar(max))
  FROM emp
  WHERE name = 'Joan'

  UNION ALL

  SELECT child.empid, child.name, parent.level + 1, 
         parent.sort_path + '/' + right('000000' + CONVERT(varchar, child.sort_order), 6) 
  FROM emp as child
    JOIN tree parent on parent.empid = child.mgrid
)
SELECT * 
FROM tree
order by sort_path;

sort_path的表达式看起来很复杂,因为SQL Server(至少是您使用的版本)没有简单的函数来格式化带前导零的数字。在Postgres中,我会使用整数数组,因此不需要转换为varchar - 但这在SQL Server中也不起作用。

答案 1 :(得分:0)

用户“a_horse_with_no_name”给出的选项对我有用。我更改了代码并将其应用于菜单生成器查询,并且它第一次工作。这是代码:

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;

答案 2 :(得分:0)

获取最近10天的日期:

SELECT DISTINCT RecordDate = DATEADD(DAY,-number,CAST(GETDATE() AS DATE)) 
FROM master..[spt_values] 
WHERE number BETWEEN 1 AND 10