简体   繁体   中英

oracle 12c using subquery factoring clause with plsql declaration

I'm a big fan of the subquery factoring clause.

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

and oracle 12c now includes a PL/SQL declaration section in the WITH clause

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

but I can't seem to get them to work together is it possible?

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;

Please refer to the syntax:
https://docs.oracle.com/database/121/SQLRF/statements_10002.htm#SQLRF01702

在此输入图像描述

plsql_declarations
在此输入图像描述

subquery_factoring_clause 在此输入图像描述

As you see, the syntax is:

WITH [ plsql_declarations ] [ subquery_factoring_clause ]

This means that PL/SQL must go first, then a rest of SQL query, in this way:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM