久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      sql語句中的with as該怎么用

      WITH AS短語,也叫做子查詢部分,可以定義一個SQL片斷,該SQL片斷會被整個SQL語句用到??梢允筍QL語句的可讀性更高,也可以在UNION ALL的不同部分,作為提供數(shù)據(jù)的部分。

      sql語句中的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), …

      –相當(dāng)于建了個e臨時表

      with e as (select * from scott.emp e where e.empno=7499) select * from e;

      –相當(dāng)于建了e、d臨時表

      with e as (select * from scott.emp), d as (select * from scott.dept) select * from e, d where e.deptno = d.deptno;

      其實(shí)就是把一大堆重復(fù)用到的sql語句放在with as里面,取一個別名,后面的查詢就可以用它,這樣對于大批量的sql語句起到一個優(yōu)化的作用,而且清楚明了。

      向一張表插入數(shù)據(jù)的with as用法

      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需要有關(guān)聯(lián)條件,不然結(jié)果會是笛卡爾積。

      with as 相當(dāng)于虛擬視圖。

      with as短語,也叫做子查詢部分(subquery factoring),可以讓你做很多事情,定義一個sql片斷,該sql片斷會被整個sql語句所用到。有的時候,是為了讓sql語句的可讀性更高些,也有可能是在union all的不同部分,作為提供數(shù)據(jù)的部分。

      特別對于union all比較有用。因?yàn)閡nion all的每個部分可能相同,但是如果每個部分都去執(zhí)行一遍的話,則成本太高,所以可以使用with as短語,則只要執(zhí)行一遍即可。如果with as短語所定義的表名被調(diào)用兩次以上,則優(yōu)化器會自動將with as短語所獲取的數(shù)據(jù)放入一個temp表里,如果只是被調(diào)用一次,則不會。而提示materialize則是強(qiáng)制將with as短語里的數(shù)據(jù)放入一個全局臨時表里。很多查詢通過這種方法都可以提高速度。

      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語句的優(yōu)點(diǎn):

      (1). SQL可讀性增強(qiáng)。比如對于特定with子查詢?nèi)€有意義的名字等。

      (2)、with子查詢只執(zhí)行一次,將結(jié)果存儲在用戶臨時表空間中,可以引用多次,增強(qiáng)性能。

      舉例:在進(jìn)行導(dǎo)入EXCEL的過程中,有時候,需要將數(shù)據(jù)存儲在臨時表中,當(dāng)下一次在進(jìn)行導(dǎo)入的時候,進(jìn)行清除臨時表的數(shù)據(jù),但是這時候,有時候發(fā)生并發(fā)問題的話,兩個用戶可能會分別操作對方的數(shù)據(jù),所以,可能造成混亂,但是可以使用WITH函數(shù)和UNION語句拼接一個SQL語句,存儲在SESSION中,當(dāng)需要導(dǎo)出錯誤信息的時候,可以使用該語句構(gòu)造數(shù)據(jù)。

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號