原文地址:https://www.cnblogs.com/linjiqin/archive/2013/06/24/3152667.html

with as语法
–针对一个别名
with tmp as (select * from tb_name)

–针对多个别名
with
   tmp as (select * from tb_name),
   tmp2 as (select * from tb_name2),
   tmp3 as (select * from tb_name3),
   …

1
2
3
4
5
6
7
8
9
--相当于建了个e临时表
with e as (select * from scott.emp e where e.empno=7499)
select * from e;
 
--相当于建了e、d临时表
with
     e as (select * from scott.emp),
     d as (select * from scott.dept)
select * from e, d where e.deptno = d.deptno;

其实就是把一大堆重复用到的sql语句放在with as里面,取一个别名,后面的查询就可以用它,这样对于大批量的sql语句起到一个优化的作用,而且清楚明了。

向一张表插入数据的with as用法

1
2
3
4
5
insert into table2
with
    s1 as (select rownum c1 from dual connect by rownum <= 10),
    s2 as (select rownum c2 from dual connect by rownum <= 10)
select a.c1, b.c2 from s1 a, s2 b where...;

select s1.sid, s2.sid from s1 ,s2需要有关联条件,不然结果会是笛卡尔积。
with as 相当于虚拟视图。

with as短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个sql片断,该sql片断会被整个sql语句所用到。有的时候,是为了让sql语句的可读性更高些,也有可能是在union all的不同部分,作为提供数据的部分。
  
特别对于union all比较有用。因为union all的每个部分可能相同,但是如果每个部分都去执行一遍的话,则成本太高,所以可以使用with as短语,则只要执行一遍即可。如果with as短语所定义的表名被调用两次以上,则优化器会自动将with as短语所获取的数据放入一个temp表里,如果只是被调用一次,则不会。而提示materialize则是强制将with as短语里的数据放入一个全局临时表里。很多查询通过这种方法都可以提高速度。

1
2
3
4
5
6
7
8
9
10
with
    sql1 as (select to_char(a) s_name from test_tempa),
    sql2 as (select to_char(b) s_name from test_tempb where not exists (select s_name from sql1 where rownum=1))
select * from sql1
union all
select * from sql2
union all
select 'no records' from dual
       where not exists (select s_name from sql1 where rownum=1)
       and not exists (select s_name from sql2 where rownum=1);

with as优点
增加了sql的易读性,如果构造了多个子查询,结构会更清晰;
更重要的是:“一次分析,多次使用”,这也是为什么会提供性能的地方,达到了“少读”的目标

update的with写法比较特殊。

错误的写法:

with sql1 as (select substr(sequence,5,28) as sequence ,

checkresultflag from dz_resultfromboss_dz dz1

where dz1.innetcode=006

and scpdateandtime>20130726000000 )

update sj_fileprocresult_0803 sj set sj.dzstatus = (select checkresultflag  from sql1

where sj.sequence=sequence
             );

上述写法会报ORA-00928: 缺失 SELECT 关键字

修改如下:

SQL> update sj_fileprocresult_0803 sj set sj.dzstatus = (
           with sql1 as (select substr(sequence,5,28) as sequence ,

checkresultflag from dz_resultfromboss_dz dz1

where dz1.innetcode=006

and scpdateandtime>20130726000000 )

select checkresultflag  from sql1

where sj.sequence=sequence
             );

[转]关于oracle with as用法的更多相关文章

  1. ORACLE 中ROWNUM用法总结(转)

    ORACLE 中ROWNUM用法总结! 对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=) ...

  2. ORACLE 中ROWNUM用法总结!

    ORACLE 中ROWNUM用法总结! 对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=) ...

  3. [转]ORACLE的ProC用法讲解

    pro*c是高级的用法,OCI是oracle的基础用法 如何编译.pc文件: proc code=cpp  parse=none iname=filename.pc oname=filename.cp ...

  4. 【转】关于oracle with as用法

    原文链接:关于oracle with as用法 with as语法–针对一个别名with tmp as (select * from tb_name) –针对多个别名with   tmp as (se ...

  5. Oracle CASE WHEN 用法介绍[Z]

    Oracle CASE WHEN 用法介绍 1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ...

  6. 转:ORACLE 中ROWNUM用法总结!

    oracle 分页查询语句:select * from (select u.*,rownum r from (select * from userifno) u where rownum<大值) ...

  7. oracle expdp/impdp 用法详解

    http://hi.baidu.com/hzfsai/item/4a4b3fc4b1cf7e51ad00efbd oracle expdp/impdp 用法详解 Data Pump 反映了整个导出/导 ...

  8. 问题:oracle case when;结果:Oracle CASE WHEN 用法介绍

    Oracle CASE WHEN 用法介绍 1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ...

  9. 数据库Oracle的select用法(部分)

    Oracle的select用法(部分): 1.查询所有: select * from employees; 2.加上where子句:用选择限制行 select * from employees whe ...

  10. Oracle ltrim() 函数用法

    Oracle ltrim() 函数用法 2015-03-21 20:42:40 Je_WangZhe 阅读数 8834更多 分类专栏: Oracle   版权声明:本文为博主原创文章,遵循CC 4.0 ...

随机推荐

  1. [转]在Linux CentOS 6.6上安装Python 2.7.9

    在Linux CentOS 6.6上安装Python 2.7.9 查看python安装版本 python -V yum中最新的也是Python 2.6.6,所以只能下载Python 2.7.9的源代码 ...

  2. C语言中 不定义结构体变量求成员大小

    所谓的求成员大小, 是求成员在该结构体中 用 sizeof(结构体名.结构体成员名) 求来的. 很多时候我们需要知道一个结构体成员中的某个成员的大小, 但是我们又不需要定义该结构体类型的变量(定义的话 ...

  3. Android Developers:绘制9-patch图片

    绘制9-patch图片工具让你使用可见即可得(WYSIWYG)编辑器轻松创建Nine Patch图像. 关于介绍Nine-path图片和它是如何工作的,请在2D Graphics的文档中查阅关于Nin ...

  4. [转]Java中Runtime.exec的一些事

    0 预备知识 1 不正确的调用exitValue 2不正确的调用waitFor 3 一种可接受的调用方式 4 调用认为是可执行程序的时候容易发生的错误 5 window执行的良好示例 6 不良好的重定 ...

  5. Java 8 – Period and Duration examples

    Few examples to show you how to use Java 8 Duration, Period and ChronoUnit objects to find out the d ...

  6. js 冷门的 label 语法

    https://github.com/Tencent/vConsole/blob/dev/src/lib/query.js#L142 https://www.cnblogs.com/hjbky/p/6 ...

  7. 微信小程序 weui 使用方法

      https://github.com/Tencent/weui-wxss/ 下载地址用于小程序的https://github.com/Tencent/weui   下载地址用于H5    运用示例 ...

  8. JDK1.7新特性,语言篇

    1. 可以用二进制表达数字 可以用二进制表达数字(加前缀0b/0B),包括:byte, short, int, long // 可以用二进制表达数字(加前缀0b/0B),包括:byte, short, ...

  9. Android开发和调试必备工具-SDK Tools

    原文链接:http://android.eoe.cn/topic/android_sdk SDK Tools是Android SDK的一个可下载部分,它包括Android SDK的开发和调试的所有工具 ...

  10. 机器学习算法实现解析——word2vec源代码解析

    在阅读本文之前,建议首先阅读"简单易学的机器学习算法--word2vec的算法原理"(眼下还没公布).掌握例如以下的几个概念: 什么是统计语言模型 神经概率语言模型的网络结构 CB ...