1

DMLWITH语句写了这个简单的例子:

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

小提琴

4

2 回答 2

3

如此处所述:https ://forums.oracle.com/thread/696477 和此处:带有子句无效标识符 oracle 的插入语句

WITH 子句是 select statemant 的一部分,因此只需尝试使用如下 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);
于 2013-07-24T15:05:22.333 回答
1

只是放在insert上面。

18:04:23 HR@sandbox> create table t (k number, v number);

Table created.

Elapsed: 00:00:00.84
23:00:22 HR@sandbox> 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
于 2013-07-24T15:02:00.630 回答