2013-07-24 111 views
1

我已經寫了有WITH聲明這個簡單的例子DML的Oracle 11g:INSERT SELECT與語句

WITH seq AS 
(SELECT to_char(LEVEL - 1) p FROM dual CONNECT BY LEVEL <= 10) 
SELECT old_value, 
     new_value 
    FROM (SELECT to_char(t1.p) old_value, 
       to_char(t2.p) new_value 
      FROM (SELECT row_number() over(ORDER BY p) rn, 
         p 
        FROM seq) t1, 
       (SELECT row_number() over(ORDER BY dbms_random.random) rn, 
         p 
        FROM seq) t2 
     WHERE t1.rn = t2.rn); 

有沒有辦法使用WITH聲明INSERT聲明?

FIDDLE

回答

3

這裏提到:https://forums.oracle.com/thread/696477 這裏:insert statement with with clause invalid identifier oracle

WITH子句是選擇衆所周知聲明的一部分,所以只要嘗試用下面的INSERT語句來包裝這一切:

INSERT INTO SOME_TABLE  
WITH seq AS 
    (SELECT to_char(LEVEL - 1) p FROM dual CONNECT BY LEVEL <= 10) 
    SELECT old_value, 
      new_value 
     FROM (SELECT to_char(t1.p) old_value, 
        to_char(t2.p) new_value 
       FROM (SELECT row_number() over(ORDER BY p) rn, 
          p 
         FROM seq) t1, 
        (SELECT row_number() over(ORDER BY dbms_random.random) rn, 
          p 
         FROM seq) t2 
      WHERE t1.rn = t2.rn); 
1

只要把insert頂部。

18:04:23 [email protected]> create table t (k number, v number); 

Table created. 

Elapsed: 00:00:00.84 
23:00:22 [email protected]> insert into t 
23:00:34 2 with tt as (
23:00:39 3 select rownum, rownum*10 from dual 
23:00:47 4 connect by rownum < 10 
23:00:54 5 ) 
23:00:55 6 select * from tt 
23:00:58 7/

9 rows created. 

Elapsed: 00:00:00.24