Oracle PL/SQL with Query

Source: Internet
Author: User

Why use?

1. If you need to apply the same query multiple times in a complex query, use with to implement code reuse;

2. With query is similar to retaining the query results to the user's temporary table. Io can be reduced in large and complex queries, and performance optimization is required.

 

What are the restrictions and features of with query?

1. If the current schema has the same table as the with query alias, the tables generated by the with query in the query take precedence;

2. It can only be used for select statements;

3. With can contain one or more queries;

4. The with query can be referenced by other queries or with queries.

 

Example:

Duzz $ Scott @ orcl> select * from Dept; </P> <p> deptno dname loc <br/> ---------- --------------- ---------- <br/> 10 Accounting New York <br/> 20 research Dallas <br/> 30 Chicago sales <br/> 40 operations Boston </P> <p> elapsed: 00:00:00. 00 <br/> duzz $ Scott @ orcl> with dept as (select 1 A from dual) Select * from Dept; </P> <p> A <br/> ---------- <br/> 1 </P> <p> elapsed: 00:00:00. 00 <br/> duzz $ Scott @ orcl> with dept as (select 1 A from dual) delete from Dept where a = 1; <br/> with dept as (select 1 A from dual) delete from Dept where a = 1 <br/> * <br/> error at line 1: <br/> ORA-00928: Missing select keyword </P> <p> elapsed: 00:00:00. 01 <br/> duzz $ Scott @ orcl> with WT1 AS (select 1 A, 2 B from dual), WT2 as (select 1 C, 3 D from dual) select * From WT1, WT2 where wt1.a = wt2.c; </P> <p> a B C D <br/> ---------- <br/> 1 2 1 3 </P> <p> elapsed: 00:00:00. 00 <br/> duzz $ Scott @ orcl> with WT1 AS (select 10 A, 2 B from dual), WT2 as (select deptno, LOC from Dept, WT1 where deptno = a) Select LOC from WT2; </P> <p> loc <br/> --------------------------------------- <br/> New York </P> <p> elapsed: 00:00:00. 00 <br/> duzz $ Scott @ orcl> <br/>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.