如何使用一个过程将两个表插入

时间:2019-04-05 21:25:34

标签: oracle plsql

我想仅使用一个过程将值插入EVENT表和EventCost表中。

事件的PK为EventID,它是FK表的EventCost。 我尝试对SCOPE_IDENTITY()使用EventID,但仍然无法创建该过程。

2)我是否需要在标头中声明v_EventID两次? (一个用于事件表,一个用于eventCost表)

CREATE OR REPLACE PROCEDURE PRC_ADD_OVER_BUDGET_EVENT
(  V_eventID IN VARCHAR
  ,V_EventType IN VARCHAR
  ,V_EventBudget IN NUMBER
  ,V_organizerID IN VARCHAR
  ,v_CostID IN VARCHAR
  ,v_FoodBeverage IN NUMBER
  ,v_wine IN NUMBER 
  ,v_Decoration IN NUMBER
  ,v_rentalfee IN NUMBER
  ,v_facility IN NUMBER
  ,v_EventID IN VARCHAR)

AS
BEGIN

INSERT INTO Event 
values 
(next_eventid_seq,null,v_eventbudget,null,null,null,v_organizerID)


INSERT INTO EventCost 
values 
(next_Costid_seq,v_FoodBeverage,v_Wine,v_Decoration,v_RentalFee,v_Facility,SCOPE_IDENTITY())

End;
/

1 个答案:

答案 0 :(得分:1)

使用returning into子句:

CREATE OR REPLACE PROCEDURE PRC_ADD_OVER_BUDGET_EVENT
(  V_eventID IN VARCHAR
  ,V_EventType IN VARCHAR
  ,V_EventBudget IN NUMBER
  ,V_organizerID IN VARCHAR
  ,v_CostID IN VARCHAR
  ,v_FoodBeverage IN NUMBER
  ,v_wine IN NUMBER 
  ,v_Decoration IN NUMBER
  ,v_rentalfee IN NUMBER
  ,v_facility IN NUMBER
  ,v_EventID IN VARCHAR)

AS
  event_ID varchar2(64);
BEGIN

INSERT INTO Event 
values 
(next_eventid_seq,null,v_eventbudget,null,null,null,v_organizerID) returning eventID into event_ID;


INSERT INTO EventCost 
values 
(next_Costid_seq,v_FoodBeverage,v_Wine,v_Decoration,v_RentalFee,v_Facility,event_ID);

End;
/

P.S。还有一些注意事项:

  1. 使用varchar2数据类型代替varchar
  2. 插入时,请提供要插入的列的名称:insert into Event (col1, col2, col3, ...)。如果您的表结构发生了更改,insert语句仍将起作用。