<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

    --- 博客已遷移至: http://www.sealyu.com/blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

    with
    sql1 as (select to_char(a) s_name from test_tempa),
    R ]F5^5"KFW0n B0sql2 as (select to_char(b) s_name from test_tempb where not exists (select s_name from sql1 where rownum=1))
    select * from sql1ITPUB個人空間2g N*` O3y2eB Q6}
    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;

    其實就是把一大堆重復(fù)用到的SQL語句放在with as 里面,取一個別名,后面的查詢就可以用它

    這樣對于大批量的SQL語句起到一個優(yōu)化的作用,而且清楚明了


    這是搜索到的英文文檔資料(說得比較全,但是本人英文特菜,還沒具體了解到,希望各高手具體談?wù)勥@個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”:

    另一個例子:
    with tempDeptName(deptName) as
    (
          select
                 dept_name as deptName
         from
                 bas_dept as dept,tpp_materialmuster as muster
         where
                 dept.DEPT_ID = muster.NEEDUNIT
               
         union all
         
         select
                 corp_name as deptName
         from
                  bas_corp as corp,tpp_materialmuster as muster
         where
                 corp.corp_id = muster.NEEDUNIT
               
    ),
    tempProjInfo(projName, projCode) as
    (
          select
                 etfprojName as projName,
                etfprojCode as projCode
         from
                 tbi_etfproj as etf, tpp_materialMuster as muster
         where
                 etf.etfprojid = muster.projid
               
         union all
         
         select
                 etmprojName as projName,
                etmprojCode as projCode
         from
                  tbi_etmproj as etm, tpp_materialMuster as muster
         where
                   etm.etmprojId = muster.projid
    )

    select
                       deptname,
                       projname,
                       projcode
          from     tpp_materialmuster as muster,tempDeptName,tempProjInfo
    posted on 2009-11-12 11:12 seal 閱讀(8511) 評論(0)  編輯  收藏 所屬分類: 數(shù)據(jù)庫
    主站蜘蛛池模板: 72pao国产成视频永久免费| 亚洲JLZZJLZZ少妇| 亚洲ts人妖网站| 国产成人人综合亚洲欧美丁香花 | 国产亚洲视频在线播放| 久久精品国产精品亚洲艾草网| 久久亚洲AV成人无码软件 | 亚洲理论电影在线观看| 亚洲综合一区二区| 亚洲日本一线产区和二线产区对比| 国产成人久久精品亚洲小说| aa在线免费观看| 国内精品免费麻豆网站91麻豆| 日韩免费无砖专区2020狼| 国产亚洲精品不卡在线| 亚洲欧洲春色校园另类小说| 亚洲丰满熟女一区二区哦| 99精品免费视频| 成年女人免费视频播放77777| 亚洲精品偷拍视频免费观看| 久久亚洲国产成人精品性色| 亚洲а∨精品天堂在线| 免费无码作爱视频| 免费观看一级毛片| 国产亚洲精品国产| 亚洲综合精品伊人久久| 最新亚洲成av人免费看| 妞干网免费观看视频| 亚洲精品夜夜夜妓女网| 亚洲色成人WWW永久在线观看 | 国产偷国产偷亚洲清高APP| 免费在线看黄网站| 永久免费av无码网站大全| 亚洲av永久无码精品古装片| 亚洲色欲色欱wwW在线| 成人片黄网站色大片免费观看APP| 成人超污免费网站在线看| 亚洲成av人在线视| 久久亚洲精品高潮综合色a片| 污污网站18禁在线永久免费观看| 国产午夜鲁丝片AV无码免费 |