[轉]關於oracle with as用法

來源:互聯網
上載者:User

標籤:有用   nec   ace   lsp   暫存資料表   cto   ocr   set   重要   

原文地址: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),
   …

123456789 --相當於建了個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用法

12345 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短語裡的資料放入一個全域暫存資料表裡。很多查詢通過這種方法都可以提高速度。

12345678910 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用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.