【www.arisingsemi.com--软件制图】

ensure用法
ORACLE WITH AS 用法

有两张表,分别为A、B,求得一个字段的值先在表A中寻找,如果A表中存在数据,则输出A表的值;如果A表中不存在,则在B表中寻找,若B表中有相应记录,则输出B表的值;如果B表中也不存在,则输出"no records”字符串。
view plaincopy to clipboardprint?
1. with 
2. sql1 as (select to_char(a) s_name from test_tempa), 
3. sql2 as (select to_char(b) s_name from test_tempb where not exists (select s_name from sql1 where rownum=1)) 
4. select * from sql1 
5. union all 
6. select * from sql2 
7. union all 
8. select "no records" from dual 
9.        where not exists (select s_name from sql1 where rownum=1) 
10.        and not exists (select s_name from sql2 where rownum=1); 

再举个简单的例子
with a as (select * from test)
select * from a;
其实就是把一大堆重复用到的SQL语句放在with as 里面,取一个别名,后面的查询就可以用它
这样对于大批量的SQL语句起到一个优化的作用,而且清楚明了
下面是搜索到的英文文档资料
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_nameAS(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”:
WITHsum_sales AS select /*+ materialize */ sum(quantity) all_sales from storesnumber_stores AS select /*+ materialize */ count(*) nbr_stores from storessales_by_store ASselect /*+ materialize */ store_name, sum(quantity) store_sales from store natural join salesSELECTstore_nameFROMstore,sum_sales,number_stores,sales_by_storewherestore_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 hierarchyThe 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_nameAS(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”:
=================================================================================
下面自己小试一把,当然,一点都不复杂,很简单很简单的例子,呵呵。
view plaincopy to clipboardprint?
1. SQL> create table t2(id int);  
2.   
3. Table created.  
4.   
5. SQL> create table t3(id int);  
6.   
7. Table created.  
8.   
9. SQL> insert into t2 values(1);  
10.   
11. 1 row created.  
12.   
13. SQL> insert into t2 values(2);  
14.   
15. 1 row created.  
16.   
17. SQL> insert into t3 values(3);  
18.   
19. 1 row created.  
20.   
21. SQL> commit;  
22.   
23. Commit complete.  
24.   
25. SQL> select * from t2;  
26.   
27.         ID  
28. ----------  
29.          1  
30.          2  
31.   
32. SQL> select * from t3;  
33.   
34.         ID  
35. ----------  
36.          3  
37. SQL> with  
38.   2  sql1 as (select * from t2),  。

本文来源:http://www.arisingsemi.com/it/83578/