我用DML
语句写了这个简单的例子WITH
:
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
语句?
答案 0 :(得分:3)
如上所述:https://forums.oracle.com/thread/696477 在这里:insert statement with with clause invalid identifier 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);
答案 1 :(得分: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