`

pl sql test

 
阅读更多

dbms_utility.get_time

http://www.java2s.com/Code/Oracle/Select-Query/ALL.htm

 

1、with table as 相当于建个临时表(用于一个语句中某些中间结果放在临时表空间的SQL语句),Oracle 9i 新增WITH语法,可以将查询中的子查询命名,放到SELECT语句的最前面。

语法就是
with tempname as (select ....)
select ...

例子:
with t as (select * from emp where depno=10)
select * from t where empno=xxx

with
wd as (select did,arg(salary) 平均工资 from work group by did),
em as (select emp.*,w.salary from emp left join work w on emp.eid = w.eid)
select * from wd,em where wd.did =em.did and wd.平均工资>em.salary;



2、何时被清除
临时表不都是会话结束就自动被PGA清除嘛! 但with as临时表是查询完成后就被清除了!
23:48:58 SCOTT@orcl> with aa as(select * from dept)
23:57:58   2  select * from aa;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

已用时间:  00: 00: 00.12
23:58:06 SCOTT@orcl> select * from aa;
select * from aa
              *
第 1 行出现错误:
ORA-00942: 表或视图不存在


已用时间:  00: 00: 00.02
23:58:14 SCOTT@orcl>

3、就这一功能来说,子查询就可以达到啊,为什么要用with呢? 用with有什么好处?
都能写,但执行计划不同的。当有多个相似子查询的时候,用with写公共部分,因为子查询结果在内存临时表中,执行效率当然就高啦~

4、问题:
有张表数据如下:
aaa 高
bbb 低
aaa 低
aaa 高
bbb 低
bbb 高
需要得到下列结果,
  高 低
aaa 2 1
bbb 1 2
问 SQL 语句怎么写??

答案:
with tt as (
  select 'aaa' id, '高' value from dual union all
  select 'bbb' id, '低' value from dual union all
  select 'aaa' id, '低' value from dual union all
  select 'aaa' id, '高' value from dual union all
  select 'bbb' id, '低' value from dual union all
  select 'bbb' id, '高' value from dual)
SELECT id,
       COUNT(decode(VALUE, '高', 1)) 高,
       COUNT(decode(VALUE, '低', 1)) 低
  FROM tt
 GROUP BY id;
===================================================================
扩展:
Oracle9i新增WITH语法,可以将查询中的子查询命名,放到SELECT语句的最前面。

  一个简单的例子:

SQL> WITH
2 SEG AS (SELECT SEGMENT_NAME, SUM(BYTES)/1024 K FROM USER_SEGMENTS GROUP BY SEGMENT_NAME),
3 OBJ AS (SELECT OBJECT_NAME, OBJECT_TYPE FROM USER_OBJECTS)
4 SELECT O.OBJECT_NAME, OBJECT_TYPE, NVL(S.K, 0) SIZE_K
5 FROM OBJ O, SEG S
6 WHERE O.OBJECT_NAME = S.SEGMENT_NAME (+)
7 ;
OBJECT_NAME OBJECT_TYPE SIZE_K
------------------------------ ------------------- ----------
DAIJC_TEST TABLE 128
P_TEST PROCEDURE 0
IND_DAIJC_TEST_C1 INDEX 128

  通过WITH语句定义了两个子查询SEG和OBJ,在随后的SELECT语句中可以直接对预定义的子查询进行查询。从上面的例子也可以看出,使用WITH语句,将一个包含聚集、外连接等操作SQL清晰的展现出来。

  WITH定义的子查询不仅可以使查询语句更加简单、清晰,而且WITH定义的子查询还具有在SELECT语句的任意层均可见的特点。

  即使是在WITH的定义层中,后定义的子查询都可以使用前面已经定义好的子查询:

SQL> WITH
2 Q1 AS (SELECT 3 + 5 S FROM DUAL),
3 Q2 AS (SELECT 3 * 5 M FROM DUAL),
4 Q3 AS (SELECT S, M, S + M, S * M FROM Q1, Q2)
5 SELECT * FROM Q3;
S M S+M S*M
---------- ---------- ---------- ----------
8 15 23 120

  利用WITH定义查询中出现多次的子查询还能带来性能提示。Oracle会对WITH进行性能优化,当需要多次访问WITH定义的子查询时,Oracle会将子查询的结果放到一个临时表中,避免同样的子查询多次执行,从而有效的减少了查询的IO数量。

WITH能用在SELECT语句中,UPDATE和DELETE语句也是支持WITH语法的,只是需要版本支持:
http://www.oracle.com.cn/viewthread.php?tid=83530

=============================================================================
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 a as (select * from test)

select * from a;

其实就是把一大堆重复用到的SQL语句放在with as 里面,取一个别名,后面的查询就可以用它

这样对于大批量的SQL语句起到一个优化的作用,而且清楚明了


这是搜索到的英文文档资料(说得比较全,但是本人英文特菜,还没具体了解到,希望各高手具体谈谈这个with
as 的好处)

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 PL/SQL测试题目和详细答案

    ### Oracle PL/SQL测试题目与知识点解析 #### 一、选择题知识点解析 **1. Oracle数据库中为新创建的表分配的初始空间通常为多大?** - **知识点解析:** Oracle数据库为新创建的表分配的空间单位是“区”(Extent...

    pl sql批量执行多个sql文件和存储过程

    ### PL/SQL批量执行多个SQL文件和存储过程 在日常的数据库管理与开发工作中,经常需要执行大量的SQL脚本或调用多个存储过程。对于Oracle数据库而言,PL/SQL是一种非常强大的工具,它不仅可以用于编写复杂的数据库...

    pl sql developer调试存储过程及调试包中创建的存储过程

    PL/SQL Developer 调试存储过程及调试包中创建的存储过程 PL/SQL Developer 调试存储过程是指使用 PL/SQL Developer 工具来调试 Oracle 数据库中的存储过程。调试存储过程可以帮助开发者快速地定位和解决存储过程...

    pl sql developer 调试存储过程.doc

    在 PL/SQL Developer 中,可以右键点击 TEST,将出现 TEST WINDOWS 窗口。在这里,可以输入参数值,因为 varstr 是输入参数,所以需要输入一个值,但是 io_cursor 是输入输出参数,在这里不需要预输入值。 3. 开始...

    pl sql经验积累

    ### PL/SQL 开发经验积累 #### 一、快捷键使用技巧 在使用 PL/SQL Developer 工具的过程中,熟练掌握快捷键可以极大提高工作效率。下面列出了一些常用的快捷键: - **撤销操作**:`Ctrl+Z` - **恢复操作**:`...

    Oracle PL-SQL 中联模拟笔试题

    而“Oracle笔试题.doc”和“Oracle Basic Test.doc”则可能包含了从基础到进阶的各类问题,帮助全面评估你的Oracle PL-SQL技能。 最后,“考题”文件可能包含了一些额外的练习题,用于巩固你在学习过程中的理解和...

    动态PL/SQL用法例子

    在动态PL/SQL的例子中,首先展示了如何使用`EXECUTE IMMEDIATE`来创建一个名为`test_qiu`的表: ```plsql BEGIN EXECUTE IMMEDIATE 'CREATE TABLE test_qiu (id NUMBER)'; END; ``` 这段代码的关键在于`EXECUTE ...

    Oracle PL/SQL PRofiler应用指南

    1. **Test Window**:选择要测试的PL/SQL代码,点击Test按钮即可开始Profiler。 2. **Profiler窗口**:在Profiler窗口中,可以查看到各个单元的具体执行情况,包括单元名称、行号、总耗时、执行次数等信息。 3. **...

    PL_SQL培训文档

    【PL/SQL培训文档】 PL/SQL,全称为Procedural Language/Structured Query Language,是Oracle数据库中的编程语言,结合了SQL(结构化查询语言)的查询能力与过程性编程语言的功能。它允许开发者编写复杂的数据库...

    pl/sql

    ### PL/SQL:高效与可移植的编程语言 PL/SQL(Procedural Language for SQL)是Oracle数据库中的一种过程化编程语言,它结合了SQL的强数据操作能力与传统编程语言的过程控制功能,旨在提高应用程序的性能和可移植性...

    PL/SQL Developer8.04官网程序_keygen_汉化

     至此,test_procedure存储过程已经完成,经过编译后就可以在其他PL/SQL块或者过程中调用了。  函数与过程具有很大的相似性,此处不再详述。 编辑本段 游标  游标的定义为:用游标来指代一个DML SQL操作返回的...

    PL-SQL 从入门到精通 经典技术

    ### PL-SQL 从入门到精通经典技术 #### 一、Oracle数据库开发工具与标准 在Oracle数据库领域,存在多种开发工具和技术标准,包括但不限于: 1. **Pro*C/C++**:这是一种较早期的数据库访问方式,适用于C/C++语言...

    Oracle及PL\SQL入门语句

    Oracle数据库和PL/SQL是IT领域中用于管理和处理数据的重要工具。PL/SQL是Oracle数据库特有的编程语言,它结合了SQL的查询能力与过程性编程语言的功能,使得开发者能够编写复杂的数据库应用程序。以下是对给定文件中...

    PL_SQL Dev7.0教程

    ### PL/SQL Developer 7.0 教程关键知识点解析 #### 一、概述 **PL/SQL Developer** 是一款专为 Oracle 数据库设计的强大开发工具,尤其在 PL/SQL 方面表现出色。该工具提供了丰富的功能来帮助开发者编写、调试、...

    Oracle (pl/sql)

    Oracle PL/SQL是Oracle数据库系统中的一个核心组件,它是一种过程化编程语言,用于扩展和增强SQL的功能。PL/SQL的全称是Procedural Language/Structured Query Language,结合了SQL的查询能力与过程编程语言的特点,...

    PL/SQL 创建 oracle 任务调度

    在PL/SQL环境中,有两种主要的方法来创建Oracle任务调度:可视化创建和通过代码创建。 **方法一:直接在PL/SQL中可视化创建** 此方法适合对界面操作熟悉的用户。在PL/SQL Developer工具中,你可以直接通过图形化...

    如何确保Oracle和PL/SQL实现正确的连接

    Oracle数据库和PL/SQL Developer是开发和管理Oracle数据库的重要工具。确保它们正确连接涉及多个步骤,包括客户端软件的选择、版本匹配、配置文件的管理和环境变量的设置。以下将详细阐述这些知识点。 首先,关于...

Global site tag (gtag.js) - Google Analytics