Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql with递归语句必须始终位于with语句链的顶部吗?_Sql - Fatal编程技术网

Sql with递归语句必须始终位于with语句链的顶部吗?

Sql with递归语句必须始终位于with语句链的顶部吗?,sql,Sql,with recursive必须始终位于with语句链的顶部吗 with a as( values(1) ), recursive t(n) as( values(1) union all select n+1 from t where n<10 ), u as( select * from t ) select * from u 在本例中,with recursive出现错误,而带有u的语句

with recursive必须始终位于with语句链的顶部吗

with a as(
    values(1)
), recursive t(n) as(
        values(1)
    union all
        select n+1
        from t
        where n<10
), u as(
    select *
    from t
)
select *
from u

在本例中,with recursive出现错误,而带有u的语句没有问题。

假设使用postgresql,因为在应用于问题的3个标记中,这最能反映您使用的语法-第一个CTE不必是递归的,但如果任何CTE是递归的,您必须在开始时定义它,所以你的例子是:

with recursive a as(
    values (1)
), t(n) as(
        values (1)
    union all
        select n+1
        from t
        where n<10
), u as(
    select *
    from t
)
select *
from u
如果SQL Server标记是正确的,那么答案仍然是递归cte不必首先出现,例如

with a as(
   select n from (values (1)) n (N)
), t(n) as(
        select n from (values (1)) n (N)
    union all
        select n+1
        from t
        where n<10
), u as(
    select *
    from t
)
select *
from u;

您使用的是MS SQL Server、Postgresql还是DB2?我删除了不兼容的数据库标记。有你名气的人应该知道如何恰当地标记问题。标记你真正使用的数据库。我正在使用它们。