将SQL Server递归WITH子句转换为Oracle 10g

时间:2010-11-09 01:34:35

标签: sql-server oracle oracle10g

我对此查询有疑问

DECLARE @INPUT INT
SET     @INPUT = 12345

;
WITH ABCD(SEQ, X, Y)
AS
(
    SELECT  1, 
            @INPUT % 10,
            @INPUT / 10

    UNION ALL

    SELECT  SEQ + 1,
            Y % 10,
            Y / 10
    FROM    ABCD
    WHERE   X > 0 OR Y > 0
)
SELECT *
FROM   ABCD
ORDER BY SEQ

此查询将生成类似这样的内容

alt text

我想将其转换为Oracle 10g(必须有效10g)

谢谢

1 个答案:

答案 0 :(得分:2)

我通过依赖字符串解析而不是数学运算来欺骗一点来计算X. 但这样的东西似乎有效

编辑:忘了第6行。我还删除了字符串作弊

with t as (
  select 12345 col1 from dual
)
select level seq, 
       trunc((col1 - 
              power(10,level) * trunc(col1/(power(10,level)))) / 
             power(10,level-1)) x,
       trunc(col1/(power(10,level))) y 
  from t
 connect by level <= length(col1)+1
/

SQL> ed
Wrote file afiedt.buf

  1  with t as (
  2    select 12345 col1 from dual
  3  )
  4  select level seq,
  5         trunc((col1 -
  6                power(10,level) * trunc(col1/(power(10,level)))) /
  7               power(10,level-1)) x,
  8         trunc(col1/(power(10,level))) y
  9    from t
 10*  connect by level <= length(col1)+1
SQL> /

       SEQ          X          Y
---------- ---------- ----------
         1          5       1234
         2          4        123
         3          3         12
         4          2          1
         5          1          0
         6          0          0

6 rows selected.