DB2子查询中的CTE

时间:2017-06-02 19:38:58

标签: recursion db2 common-table-expression db2-luw

我正在尝试在from子句中使用递归CTE。这个CTE独立工作:

with i (i) as ( 
    values (1)
    union all 
    select i + 1 from i
    where i < 3 
) 
select * from i;

            I
-------------
            1
            2
            3

但是当我在from子句中尝试时:

select * 
from (
    with i (i) as ( 
        values (1)
        union all 
        select i + 1 from i
        where i < 3 
    )
    select * from i
) i;
ERRO próximo da linha 1:
SQL0104N  An unexpected token "as" was found following "*
from (
with i (i)".  Expected tokens may include:  "JOIN".

类似的构造在Postgresql中有效。我错过了什么?

2 个答案:

答案 0 :(得分:2)

嗨“with”语句必须是db2查询中的第一个,试试这个

with i (i) as (
    values (1)
    union all
    select i + 1 from i
    where i < 3
)
select *
   from (

           select * from i
         ) i;

答案 1 :(得分:1)

作为记录,您的查询在 DB2 v11.5.4.0 中运行良好:

std::getline

产生预期:

ifstream Name_file("names.txt");

std::string line;
while (std::getline(Name_file, line))
{
  std::string firstName = line.substr(0, line.find(' '));
  //do stuff with firstName..
}

但是它在 DB2 v11.1.4.4 版本中还没有工作,正如在这个 db fiddle