简体   繁体   中英

insert collection data and fk into table oracle apex

So the basic question is: how can I retrieve the primary key from another table into other table as foreign key and at the same time insert stored input from the collections into the table?

With this process I can get the primary key from the table and insert it as foreign key in the other table:

DECLARE
  l_id table1.id%TYPE;
BEGIN
  INSERT INTO table1(val) VALUES ('Europe')
    RETURNING id INTO l_id;
  INSERT INTO table2(continent_id, val) VALUES (l_id,'Belgium');
END;
/

With this process however I can insert the stored information from the collection into the corresponding tables.

begin
for i IN (SELECT c001 AS a,c002 AS b, c003 AS c, c004 AS d
          from APEX_COLLECTIONS
  WHERE COLLECTION_NAME='ITEM_DATA'
  ORDER BY 1)
 loop
    insert into table1 (USERID,TEST1,TEST2, TEST3)
    values ( i.a,i.b,i.c,i.d);
  end loop;
apex_collection.delete_collection(p_collection_name=>'ITEM_DATA');
end;

However, I would like to develop a process in which both the foreign key and the stored information from the collections are entered into the tables at the same time.

INSERT INTO TABLE1(FKID,USERID,PROGRAMID,COURSEID) VALUES (ID_SEQ.NEXTVAL, (select c001,c002,c003 from APEX_COLLECTIONS WHERE COLLECTION_NAME='SAVED_DATA'))

Unfortunately I cannot insert a select statement in an insert into values statement.

If I have understood correctly, is this what you want?

INSERT INTO TABLE1 (FKID,USERID,PROGRAMID,COURSEID) 
 SELECT ID_SEQ.NEXTVAL, c001, c002, c003 
 FROM APEX_COLLECTIONS 
 WHERE COLLECTION_NAME='SAVED_DATA';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM