简体   繁体   English

在单个查询的 WITH 子句中包含多个内联函数和多个 CTE

[英]Include multiple inline functions and multiple CTEs in a WITH clause in a single query

Oracle 18c:甲骨文 18c:

What is the syntax for including multiple inline functions and multiple CTEs in a WITH clause in a single query?在单个查询的 WITH 子句中包含多个内联函数多个 CTE的语法是什么?

Function #1:功能#1:

function fucntion1(num in number) return number
is
begin
    return num + 1;
end;

Function #2:功能#2:

function fucntion2(num in number) return number
is
begin
    return num + 2;
end;

CTE #1: CTE #1:

cte as (select 1 from dual)

CTE #2: CTE #2:

cte2 as (select 2 from dual)

As ever for a "what is the syntax for ..." questions, you should refer to the official documentation.与以往一样,对于“...的语法是什么”问题,您应该参考官方文档。

The SELECT syntax is SELECT语法

query_block ::=查询块 ::=

query_block 语法

with_clause ::= with_clause ::=

with_clause 语法

plsql_declarations ::= plsql_declarations ::=

plsql_declarations

subquery_factoring_clause ::= subquery_factoring_clause ::=

subquery_factoring_clause 语法

The PL/SQL function syntax PL/SQL 函数语法

function_definition ::=函数定义 ::=

函数定义语法

body ::=身体 ::=

正文语法

Therefore:所以:

  • A PL/SQL function's body must be terminated with a ; PL/SQL 函数的主体必须以;结尾。 . .

    (Note: this is a PLSQL statement terminator and not a separator in the WITH clause between PL/SQL function declarations as there is no separator character following PL/SQL function declarations.) (注意:这是一个 PLSQL 语句终止符,而不是 PL/SQL 函数声明之间的WITH子句中的分隔符,因为 PL/SQL 函数声明之后没有分隔符。)

  • There is a , character between successive sub-query factoring clauses.在连续的子查询因式分解子句之间有一个,字符。

  • The SELECT statement does not need a ; SELECT语句不需要; or / statement terminator but it may be allowed/required/forbidden by the client application you are using to denote the termination of the statement./语句终止符,但您用于表示语句终止的客户端应用程序可能允许/要求/禁止它。

    For example:例如:

    • You can only pass a single statement via an OJDBC statement and, for this client, the statement terminator is forbidden.您只能通过 OJDBC 语句传递单个语句,并且对于此客户端,禁止使用语句终止符。
    • In SQL Developer, when you are running a single statement then the trailing statement terminator is allowed but is optional.在 SQL Developer 中,当您运行单个语句时,允许使用尾随语句终止符,但它是可选的。
    • In SQL Developer, when you are running a script then statement terminators are required between statements.在 SQL Developer 中,当您运行脚本时,语句之间需要语句终止符。

It looks like the syntax is:看起来语法是:

  • Use a semicolon at the end of each function, including the final function.在每个函数的末尾使用分号,包括最终函数。 (no backslash) (没有反斜杠)
  • Separate the CTEs with a comma.用逗号分隔 CTE。
  • Edited: Don't include a final semicolon or backslash in the WITH clause .编辑:不要在 WITH 子句中包含最后的分号或反斜杠。

with

function fucntion1(num in number) return number
is
begin
    return num + 1;
end;

function fucntion2(num in number) return number
is
begin
    return num + 2;
end;

cte as (select 1 from dual), 

cte2 as (select 2 from dual)

select
    fucntion1(1) as function_result
from
    cte
union all
select
    fucntion2(1)
from
    cte2

FUNCTION_RESULT
---------------
              2
              3

Related: Using an inline function and CTE in a SQL query相关: 在 SQL 查询中使用内联函数和 CTE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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