我试图一次性执行以下声明。就像我们使用INSERT ALL在一次执行中插入行一样。有没有办法可以在一次执行中执行以下两个语句(使用INSERT ALL)。
INSERT
INTO TABLE_a
(
COLUMN_1,
COLUMN_2,
COLUMN_3,
COLUMN_4,
COLUMN_5,
COLUMN_6,
COLUMN_7,
COLUMN_8
)
SELECT 1234, -- unique id for this insertion
COLUMN_2,
COLUMN_3,
COLUMN_4,
COLUMN_5,
COLUMN_6,
COLUMN_7,
COLUMN_8
FROM TABLE_B
WHERE COLUMN_9=1;
INSERT
INTO TABLE_a
(
COLUMN_1,
COLUMN_2,
COLUMN_3,
COLUMN_4,
COLUMN_5,
COLUMN_6,
COLUMN_7,
COLUMN_8
)
SELECT 1235, -- unique id for this insertion
COLUMN_2,
COLUMN_3,
COLUMN_4,
COLUMN_5,
COLUMN_6,
COLUMN_7,
COLUMN_8
FROM TABLE_B
WHERE COLUMN_9=2;
答案 0 :(得分:1)
当然,您可以使用in clause收集Id 1和2
并使用decode在1234和1235之间有所不同:
INSERT
INTO TABLE_a
(
COLUMN_1,
COLUMN_2,
COLUMN_3,
COLUMN_4,
COLUMN_5,
COLUMN_6,
COLUMN_7,
COLUMN_8
)
SELECT decode(COLUMN_9,1,1234,2,1235),
COLUMN_2,
COLUMN_3,
COLUMN_4,
COLUMN_5,
COLUMN_6,
COLUMN_7,
COLUMN_8
FROM TABLE_B
WHERE COLUMN_9 in (1,2);
答案 1 :(得分:0)
为什么不使用OR条件?如果它适合你,试试这个
INSERT
INTO TABLE_a
(
COLUMN_1,
COLUMN_2,
COLUMN_3,
COLUMN_4,
COLUMN_5,
COLUMN_6,
COLUMN_7,
COLUMN_8
)
SELECT CASE WHEN COLUMN_9 = 1 THEN 1234 WHEN COLUMN_9 = 2 THEN 1235 END,
COLUMN_2,
COLUMN_3,
COLUMN_4,
COLUMN_5,
COLUMN_6,
COLUMN_7,
COLUMN_8
FROM TABLE_B
WHERE COLUMN_9=1 OR COLUMN_9=2;
使用@ strong
建议的 DECODE() 功能INSERT
INTO TABLE_a
(
COLUMN_1,
COLUMN_2,
COLUMN_3,
COLUMN_4,
COLUMN_5,
COLUMN_6,
COLUMN_7,
COLUMN_8
)
SELECT DECODE(COLUMN_9,1,1234,2,1235)
COLUMN_2,
COLUMN_3,
COLUMN_4,
COLUMN_5,
COLUMN_6,
COLUMN_7,
COLUMN_8
FROM TABLE_B
WHERE COLUMN_9=1 OR COLUMN_9=2;
答案 2 :(得分:0)
您可以将INSERT INTO ... SELECT
与联合查询一起使用:
INSERT INTO TABLE_a (COLUMN_1, COLUMN_2, COLUMN_3, COLUMN_4, COLUMN_5, COLUMN_6,
COLUMN_7, COLUMN_8)
SELECT
1234, COLUMN_2, COLUMN_3, COLUMN_4, COLUMN_5, COLUMN_6, COLUMN_7, COLUMN_8
FROM TABLE_B
WHERE COLUMN_9 = 1
UNION ALL
SELECT
1235, COLUMN_2, COLUMN_3, COLUMN_4, COLUMN_5, COLUMN_6, COLUMN_7, COLUMN_8
FROM TABLE_B
WHERE COLUMN_9 = 2
与原始方法相比,这种方法有一个明显的优势。有了这个答案,整个插入就会发生在一个声明中。