所以我试图将自动生成的(从触发器/序列)ID存储到变量中,以便在随后的两个插入语句中使用。但是,我注意到没有数据插入第二张表( temp_calc ),并且似乎无法将相同的值( calculation_id )返回到两个不同的变量中。如何在两个插入语句中使用返回给v_calculation_id的相同值?以下是我的(尝试但不正确的)代码:
insert into calculation(calculation_id, calculation_Date, calculation_name)
VALUES(null, sysdate, 'TEST')
returning calculation_id into v_calculation_id;
--
INSERT INTO calculation_trans (calculation_id, calculation_trans_type_id, create_dt)
values (v_calculation_id, 776, sysdate);
--
insert into temp_calc (calculation_id, flag_type_id)
VALUES (v_calculation_id, 75462355);
答案 0 :(得分:1)
您需要使用PL / SQL块:
DECLARE
v_calculation_id NUMBER;
BEGIN
-- removing calculation_id(it should be IDENTITY/default SEQ/trigger)
insert into calculation( calculation_Date, calculation_name)
VALUES(sysdate, 'TEST')
returning calculation_id into v_calculation_id;
INSERT INTO calculation_trans(calculation_id,calculation_trans_type_id, create_dt)
values (v_calculation_id, 776, sysdate);
insert into temp_calc (calculation_id, flag_type_id)
VALUES (v_calculation_id, 75462355);
COMMIT;
END;
答案 1 :(得分:0)
最好尝试返回主键