`

Oracle With 语法 示例

阅读更多


WITH a AS (SELECT * FROM bd_member WHERE ROWNUM<10),
b AS (SELECT * FROM tp_trade_card)
select A.MEMBER_NAME,
B.CARD_NO
from A,
B
where A.BD_MEMBER_ID = B.BD_MEMBER_ID

 

Google Search: Oracle +with 视图就可以得到结果...

 

 

Oracle WITH clause

Oracle Tips by Burleson Consulting

 

About Oracle WITH clause

Starting in Oracle9i release 2 we see an incorporation of the SQL-99 “WITH clause”, a tool for materializing subqueries to save Oracle from having to re-compute them multiple times.

The SQL “WITH clause” is very similar to the use of Global temporary tables (GTT), a technique that is often used to improve query speed for complex subqueries. Here are some important notes about the Oracle “WITH clause”:

   • The SQL “WITH clause” only works on Oracle 9i release 2 and beyond.
   • Formally, the “WITH clause” is called subquery factoring
   • The SQL “WITH clause” is used when a subquery is executed multiple times
   • Also useful for recursive queries (SQL-99, but not Oracle SQL)

To keep it simple, the following example only references the aggregations once, where the SQL “WITH clause” is normally used when an aggregation is referenced multiple times in a query.
We can also use the SQL-99 “WITH clause” instead of temporary tables. The Oracle SQL “WITH clause” will compute the aggregation once, give it a name, and allow us to reference it (maybe multiple times), later in the query.

The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:

WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);


Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH  clause”:

WITH
sum_sales AS
  select /*+ materialize */
    sum(quantity) all_sales from stores
number_stores AS
  select /*+ materialize */
    count(*) nbr_stores from stores
sales_by_store AS
  select /*+ materialize */
  store_name, sum(quantity) store_sales from
  store natural join sales
SELECT
   store_name
FROM
   store,
   sum_sales,
   number_stores,
   sales_by_store
where
   store_sales > (all_sales / nbr_stores)
;


Note the use of the Oracle undocumented “materialize” hint in the “WITH clause”. The Oracle materialize hint is used to ensure that the Oracle cost-based optimizer materializes the temporary tables that are created inside the “WITH” clause. This is not necessary in Oracle10g, but it helps ensure that the tables are only created one time.

It should be noted that the “WITH clause” does not yet fully-functional within Oracle SQL and it does not yet support the use of “WITH clause” replacement for “CONNECT BY” when performing recursive queries.

To see how the “WITH clause” is used in ANSI SQL-99 syntax, here is an excerpt from Jonathan Gennick’s great work “Understanding the WITH Clause” showing the use of the SQL-99 “WITH clause” to traverse a recursive bill-of-materials hierarchy

The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:

WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);


Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH” clause”:

分享到:
评论

相关推荐

    oracle触发器语法要点

    ### Oracle触发器语法要点 #### 一、触发器概述 Oracle触发器是一种存储过程,它在特定的数据库事件(如INSERT、UPDATE或DELETE)发生时自动执行。触发器可以用于实施复杂的业务规则、数据完整性检查或者自动生成...

    oracle语法.rar

    Oracle数据库是全球广泛使用的大型关系型数据库管理系统之一,其语法丰富且强大,尤其在处理复杂数据操作时表现出色。这个“oracle语法.rar”压缩包包含了关于Oracle数据库的一些关键知识点,如递归查询、内置函数、...

    Oracle和SQL_Server的语法区别

    - Oracle 的 `START WITH U CONNECT BY` 用于创建层次查询,SQL Server 可以通过递归公共表表达式(CTE)或存储过程实现类似功能。 - Oracle 支持 `INTERSECT` 和 `MINUS` 集合运算符,而 SQL Server 用 `EXISTS` 和...

    oracle常用语法

    **语法示例**: ```sql RANK() OVER (PARTITION BY v.FAULT_ID ORDER BY v.dealTime DESC, v.OPERATE_SEQ DESC) ``` 这个表达式的意思是:按照 `FAULT_ID` 分组,并在每个分组内按 `dealTime` 和 `OPERATE_SEQ` 降序...

    oracle函数大全及存储过程语法 chm

    在提供的压缩包`Oracle函数大全.chm`中,你将找到关于Oracle所有内置函数的详细信息,包括每个函数的作用、语法、返回值类型和使用示例。而`oracle存储过程.chm`则涵盖了存储过程的创建、调用、修改和删除等相关知识...

    oracle_PLSQL_语法详细手册

    ### Oracle PL/SQL 语法详细手册知识点概览 #### 一、CREATE TABLE 语句 在 Oracle 数据库中,`CREATE TABLE` 语句是最基本也是最重要的 SQL 命令之一,用于创建一个新的表。表作为数据库的核心组成部分,用于存储...

    oracle授权语法

    ### Oracle授权语法详解 在Oracle数据库管理中,权限管理是一项重要的功能,它允许数据库管理员(DBA)控制用户对数据库对象的访问级别。本文将详细解释Oracle中的授权语法,并通过具体的例子来阐述如何进行不同类型...

    Oracle常用语法1-2

    根据提供的文件信息,本文将对Oracle数据库中的常用语法进行详细解析。主要涵盖用户管理、表空间操作、表结构创建与修改、注释添加以及序列管理等核心知识点。 ### 用户管理 1. **解锁用户账户**:若用户账户被...

    trigger语法经典语法全

    ### Trigger语法详解 #### 一、引言 在数据库领域,触发器(Trigger)是一种特殊类型的存储过程,它被设计用于响应对特定表的数据修改操作(如INSERT、UPDATE或DELETE)。触发器能够在这些操作发生时自动执行,...

    ORACLE的基本语法集锦

    本文将深入探讨Oracle数据库的一些基本语法,包括表、视图、同义词、存储过程、函数、触发器、游标以及序列的创建与使用,旨在为初学者提供一个全面的入门指南。 ### 表(Table) 表是数据库中最基本的数据存储单元...

    Oracle的Connect By使用示例

    通过以上示例可以看出,`START WITH...CONNECT BY`语法非常强大,它不仅能够帮助我们高效地进行树状结构数据的查询,还可以轻松构建出符合业务需求的层级视图。此外,结合其他函数(如`LPAD`和`SYS_CONNECT_BY_PATH`...

    ORACLE与SQLSERVER语法差异分析

    在数据库管理领域,ORACLE和SQL SERVER是两个广泛使用的数据库管理系统,它们在语法上有许多相似之处,但也存在一些显著的差异。以下是对标题和描述中所述知识点的详细说明: 1. **递归查询**: - 在SQL SERVER中...

    Oracle start with.connect by prior子句实现递归查询

    ### Oracle Start With.Connect By Prior 子句实现递归查询 #### 概述 在Oracle数据库中,`Start With.Connect By Prior`子句是执行递归查询的一种强大工具,主要用于处理层次结构数据。这类数据通常存在于组织...

    Oracle层次查询和with函数的使用示例

    Oracle提供了一些特性的查询语法来处理这些层级数据,包括层次查询(Hierarchical Query)和WITH函数(Common Table Expression,CTE)。下面我们将详细讨论这两个概念以及它们的使用示例。 首先,我们来看层次查询...

    oracle转DB2 对照

    - 注意点: Oracle使用 `START WITH` 和 `CONNECT BY` 进行层次查询,而DB2使用 `WITH RECURSIVE` 子句。 #### 9. 打印输出信息 - **Oracle** 和 **DB2** 都支持打印输出信息。 - Oracle示例: ```sql DBMS_...

    oracle while的用法示例分享

    当查询中多次用到某一部分时,可以用Oracle with语句创建一个公共临时表。因为子查询在内存临时表中,避免了重复解析,所以执行效率会提高不少。临时表在一次查询结束自动清除。 一般语法格式: 代码如下:with ...

    oracle.dataaccess.dll

    3. “The provider is not compatible with the version of Oracle client”:检查.NET应用程序与Oracle客户端版本的兼容性,确保两者匹配。 总的来说,Oracle.DataAccess.dll为.NET开发者提供了一种强大的工具,...

    oracle中sql语句用法

    #### 创建序列的语法示例 ```sql CREATE SEQUENCE 序列名 INCREMENT BY 增量值 START WITH 起始值 NOMAXVALUE -- 不设置最大值 NOCYCLE -- 不循环 NOCACHE -- 不缓存 ``` - **INCREMENT BY**: 指定序列每次生成的...

Global site tag (gtag.js) - Google Analytics