[转]关于oracle with as用法
原文地址: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 table2with 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 sql1union allselect * from sql2union allselect '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用法的更多相关文章
- ORACLE 中ROWNUM用法总结(转)
ORACLE 中ROWNUM用法总结! 对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=) ...
- ORACLE 中ROWNUM用法总结!
ORACLE 中ROWNUM用法总结! 对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=) ...
- [转]ORACLE的ProC用法讲解
pro*c是高级的用法,OCI是oracle的基础用法 如何编译.pc文件: proc code=cpp parse=none iname=filename.pc oname=filename.cp ...
- 【转】关于oracle with as用法
原文链接:关于oracle with as用法 with as语法–针对一个别名with tmp as (select * from tb_name) –针对多个别名with tmp as (se ...
- Oracle CASE WHEN 用法介绍[Z]
Oracle CASE WHEN 用法介绍 1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ...
- 转:ORACLE 中ROWNUM用法总结!
oracle 分页查询语句:select * from (select u.*,rownum r from (select * from userifno) u where rownum<大值) ...
- oracle expdp/impdp 用法详解
http://hi.baidu.com/hzfsai/item/4a4b3fc4b1cf7e51ad00efbd oracle expdp/impdp 用法详解 Data Pump 反映了整个导出/导 ...
- 问题:oracle case when;结果:Oracle CASE WHEN 用法介绍
Oracle CASE WHEN 用法介绍 1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ...
- 数据库Oracle的select用法(部分)
Oracle的select用法(部分): 1.查询所有: select * from employees; 2.加上where子句:用选择限制行 select * from employees whe ...
- Oracle ltrim() 函数用法
Oracle ltrim() 函数用法 2015-03-21 20:42:40 Je_WangZhe 阅读数 8834更多 分类专栏: Oracle 版权声明:本文为博主原创文章,遵循CC 4.0 ...
随机推荐
- 在Linux CentOS 6.6上安装RedisLive
Real time dashboard for redis 安装必须软件 1.安装pip到指定的python版本下面: curl -O https://bootstrap.pypa.io/get-pi ...
- mysql快速移植表数据
使用select into outfile xxx , load data infile xxx 例如 : SELECT * into outfile '/tmp/out.txt' FROM `db ...
- git log退出方法
英文状态下按Q
- 坑爹的高德地图API
症状 ld: '-[MASearch poiSearchWithOption:]' in *****/Release-iphonesimulator/libMASearchKit.a(MASearch ...
- Appium 设置手机连接方式
使用appium的 Connection 和driver 的setConnection方法,可以切换手机的上网方式 // 切换到wifi Connection cnn = Connection.WIF ...
- dubbo-2.8.4编译发布到本地Nexus库
首先修改本地Maven的settings.xml文件,注意<Servers>标签中的内容: <settings xmlns="http://maven.apache.org ...
- octave画心形曲线
octave是gnu出品和matlab兼容的科学计算软件,具有体积小,兼容性好,免费的优点. 心形曲线是根据函数:( x2 + y2 -1 )3 - x2y3=0 改编而成. clear all; c ...
- Atitit 我们的devops战略与规划 规范 推荐标准
Atitit 我们的devops战略与规划 规范 推荐标准 1. Vm容器化1 2. 热部署tomcat+jrebel 或者resin1 3. 增量更新与差异更新1 4. 补丁提取与应用2 为了方便提 ...
- Atitit 通用接口的设计与实现attilax 总结
Atitit 通用接口的设计与实现attilax 总结 1.1. 现存的情况1 1.2. 接口返回类型,与返回序列化格式1 1.3. 异常传递 代替返回值模式1 1.4. 通用接口原理1 1.5. A ...
- 使用flow捕获js错误提高代码质量
本文将介绍Flow及其主特性.下面分别从如何安装设置,如何添加类型注释,如何在运行时自动去掉注释等方面来介绍. 安装 目前Flow兼容的操作系统有Mac OS X,Linux(64位),Windows ...