我有以下光标(无论如何都是这样),我用它来复制作为多对多关系一部分的表的条目。我需要在INSERT INTO B语句期间生成的新ID,以使用。
更新联结表DECLARE
BEGIN
FOR rec IN (SELECT id
FROM A
WHERE group_id = 7)
LOOP
INSERT INTO B (b_id, thing2, ...stuff...);
INSERT INTO C (rec.id, /* new ID generated by previous insert statement */
COMMIT;
END LOOP;
END;
/
如何在第二个INSERT语句中使用插入后捕获b_id值?
答案 0 :(得分:4)
DECLARE
v_bid B.b_id%TYPE;
BEGIN
FOR rec IN (SELECT id
FROM A
WHERE group_id = 7)
LOOP
INSERT INTO B (b_id, thing2, ...stuff...) RETURNING b_id INTO v_bid;
INSERT INTO C (rec.id, /* new ID generated by previous insert statement */
COMMIT;
END LOOP;
END;
也就是说,可以在不使用游标的情况下执行此操作。