In oracle procedure how to fetch the PKId just added to be added as FK ID for next table

user2093576

In an oracle procedure, I need to insert data in EmployeeHeader table and then later insert the PK id of this table as the FK id of EmployeeDetails table. How can we achieve this?

 INSERT INTO EmployeeHeader(
              HEADER_PK_ID
                      empNo

         )
         VALUES(
                    HEADER_PK_ID_SEQ.NEXTVAL,
                    'SOMETHING'

              );

 INSERT INTO EmployeeDetails (
              DTLHEADER_PK_ID,
              HEADER_fK_ID
                      empname,
              age

         )
         VALUES(
                    DTLHEADER_PK_ID_SEQ.NEXTVAL,
                    HEADER_PK_IDn, -- (THIS NEEDS TO BE FETCHED FROM EmployeeHeader)
                    21
              );
Bob Jarvis - Reinstate Monica

Use the RETURNING clause of the INSERT statement:

DECLARE
  nHeader_pk_id  NUMBER;
BEGIN
  INSERT INTO EmployeeHeader
    (HEADER_PK_ID, EMPNO)
  VALUES
    (HEADER_PK_ID_SEQ.NEXTVAL, 'SOMETHING')
  RETURNING HEADER_PK_ID INTO nHeader_pk_id;

  INSERT INTO EmployeeDetails
    (DTLHEADER_PK_ID, HEADER_FK_ID, EMPNAME, AGE)
  VALUES
    (DTLHEADER_PK_ID_SEQ.NEXTVAL, nHeader_pk_id, 'Somebody', 21);
END;

My personal preference is to use ON INSERT triggers to handle the population of primary key fields, in the following manner:

CREATE OR REPLACE TRIGGER EMPLOYEEHEADER_BI
  BEFORE INSERT ON EMPLOYEEHEADER
  FOR EACH ROW
BEGIN
  IF :NEW.HEADER_PK_ID IS NULL THEN
    :NEW.HEADER_PK_ID := HEADER_PK_ID_SEQ.NEXTVAL;
  END IF;
END EMPLOYEEHEADER_BI;

CREATE OR REPLACE TRIGGER EMPLOYEEDETAILS_BI
  BEFORE INSERT ON EMPLOYEEDETAILS
  FOR EACH ROW
BEGIN
  IF :NEW.DTLHEADER_PK_ID IS NULL THEN
    :NEW.DTLHEADER_PK_ID := DTLHEADER_PK_ID_SEQ.NEXTVAL;
  END IF;
END EMPLOYEEDETAILS_BI;

and the INSERT statements become:

DECLARE
  nHeader_pk_id     NUMBER;
  nDtlheader_pk_id  NUMBER;
BEGIN
  INSERT INTO EmployeeHeader
    (EMPNO)  -- Note: PK field not mentioned - will be populated by trigger
  VALUES
    ('SOMETHING')
  RETURNING HEADER_PK_ID INTO nHeader_pk_id;

  INSERT INTO EmployeeDetails
    (HEADER_FK_ID, EMPNAME, AGE)  -- Note: PK field not mentioned - will be populated by trigger
  VALUES
    (nHeader_pk_id, 'Somebody', 21)
  RETURNING DTLHEADER_PK_ID INTO nDtlheader_pk_id;
END;

(I use the IF pk_field IS NULL THEN construct because I often need to copy data from production to development databases and wish to preserve any key values pulled from production to simplify debugging. If you don't have this requirement you can eliminated the IS NULL check and just assign the sequence's NEXTVAL directly to the column).

Done in this manner the application code doesn't need to know or care about which sequence is used to generate the primary key value for a particular table, and the primary key field is always going to end up populated.

Best of luck.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get added record (not just the id) through publishAdd()-notification?

From Dev

How to keep track of the entity id for the stuff that was just added to the db?

From Dev

How do find the last added ID in a non auto incremented table?

From Dev

How to get the URL of an image just added in PHPhotoLibrary

From Dev

Crop just added a picture

From Dev

Select with just added property

From Dev

How to remove suffix id added on sql server that was previously added SQL

From Dev

jQuery Copy dynamically added table row values into next row

From Dev

How to fetch the cursor data in oracle stored procedure

From Dev

How to return last added id in Laravel

From Dev

How to find item index just added into my data binding ListView

From Dev

How to change attribute of dynamically added element on just calling a function

From Dev

Dynamically insert/remove table rows (including how to give ID's to added rows)

From Dev

How to set ID or css selector for new row added in table using javascript

From Dev

How to delete a specific <td> cell with no id from a dynamically added table using jquery?

From Dev

Get just added Object with EntityManagerFactory

From Dev

How to fetch Core Data by the order the data was added and display in that order in a UITableView?

From Dev

How can fetch the row which has been recently added in the qlistwidget

From Dev

Oracle use defined type in package as record added to table of defined type

From Dev

Fetch results added today no earlier

From Dev

How to avoid widgets being added while moving back and on then clicking Next?

From Dev

record is not added to the table

From Dev

row is not getting added to table

From Dev

Get the name of an added table

From Dev

Ruby/Rails: friend_id is not added to friendships table

From Dev

Unable to get row id of dynamically added rows in table layout of android

From Dev

Ruby/Rails: friend_id is not added to friendships table

From Dev

jQuery click event for dynamically added tr's in table with specific ID

From Dev

next row is added into single cell

Related Related

  1. 1

    How to get added record (not just the id) through publishAdd()-notification?

  2. 2

    How to keep track of the entity id for the stuff that was just added to the db?

  3. 3

    How do find the last added ID in a non auto incremented table?

  4. 4

    How to get the URL of an image just added in PHPhotoLibrary

  5. 5

    Crop just added a picture

  6. 6

    Select with just added property

  7. 7

    How to remove suffix id added on sql server that was previously added SQL

  8. 8

    jQuery Copy dynamically added table row values into next row

  9. 9

    How to fetch the cursor data in oracle stored procedure

  10. 10

    How to return last added id in Laravel

  11. 11

    How to find item index just added into my data binding ListView

  12. 12

    How to change attribute of dynamically added element on just calling a function

  13. 13

    Dynamically insert/remove table rows (including how to give ID's to added rows)

  14. 14

    How to set ID or css selector for new row added in table using javascript

  15. 15

    How to delete a specific <td> cell with no id from a dynamically added table using jquery?

  16. 16

    Get just added Object with EntityManagerFactory

  17. 17

    How to fetch Core Data by the order the data was added and display in that order in a UITableView?

  18. 18

    How can fetch the row which has been recently added in the qlistwidget

  19. 19

    Oracle use defined type in package as record added to table of defined type

  20. 20

    Fetch results added today no earlier

  21. 21

    How to avoid widgets being added while moving back and on then clicking Next?

  22. 22

    record is not added to the table

  23. 23

    row is not getting added to table

  24. 24

    Get the name of an added table

  25. 25

    Ruby/Rails: friend_id is not added to friendships table

  26. 26

    Unable to get row id of dynamically added rows in table layout of android

  27. 27

    Ruby/Rails: friend_id is not added to friendships table

  28. 28

    jQuery click event for dynamically added tr's in table with specific ID

  29. 29

    next row is added into single cell

HotTag

Archive