oracle 12c使用带有plsql声明的子查询因子子句

时间:2016-07-06 17:57:24

标签: oracle oracle12c subquery-factoring

我是子查询因子条款的忠实粉丝。

WITH t1 as (select 1 as id from dual)
select * from t1;

和oracle 12c现在包含WITH子句中的PL / SQL声明部分

WITH
  FUNCTION with_function(p_id IN NUMBER) RETURN NUMBER IS
  BEGIN
    RETURN p_id;
  END;
SELECT with_function(id)
FROM   t1
WHERE  rownum = 1

但我似乎无法让他们一起工作吗?

WITH t1 as (select 1 as id from dual)
WITH  FUNCTION with_function(p_id IN NUMBER) RETURN NUMBER IS
  BEGIN
    RETURN p_id;
  END;
SELECT with_function(id)
FROM   t1
WHERE  rownum = 1;

1 个答案:

答案 0 :(得分:3)

请参阅语法:
https://docs.oracle.com/database/121/SQLRF/statements_10002.htm#SQLRF01702

enter image description here

plsql_declarations
enter image description here

subquery_factoring_clause enter image description here

如您所见,语法为:

WITH [ plsql_declarations ] [ subquery_factoring_clause ]

这意味着PL / SQL必须首先进行,然后以其他方式进行SQL查询:

WITH 
  FUNCTION with_function(p_id IN NUMBER) RETURN NUMBER IS
  BEGIN
    RETURN p_id;
  END;
t1 as (select 1 as id from dual)
SELECT with_function(id)
FROM   t1
WHERE  rownum = 1;