我想仅使用一个过程将值插入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;
/
答案 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。还有一些注意事项:
varchar2
数据类型代替varchar
。insert into Event (col1, col2, col3, ...)
。如果您的表结构发生了更改,insert语句仍将起作用。