How can I insert data into two tables simultaneously with only one sql script db2?

How do I insert multiple tables with one sql script in db2

For example, insert a row in T1 DOCK_DOORand then insert in T2 DOCK_DOOR_LANEseveral times based on dock_door_sysidthe first table.

My first approach was as follows. I tried to use a combination with three inserts. on the other hand, there is no option for inserting into the second table, and if this can be automated with a single insert. thanks for any feedback

sql example

WITH ins AS (
  INSERT INTO DBF1.DOCK_DOOR    (DOCK_DOOR_SYSID,DOOR_NUMBER,DOOR_NAME,DOCK_SYSID,DOOR_SEQ,ENCRYPTION_CODE,RFID_ENBLD_FLAG,LANES_COUNT,CMNT_TEXT,CREATE_TS,CREATE_USERID,UPDATE_TS,UPDATE_USERID,VER_NUMBER,ACTIVE_FLAG,STATUS_SYSID,DOOR_TYPE_SYSID) 
VALUES (nextval for DBF1.DOCK_DOOR_SEQ,'026','DOOR025',61,25,NULL,'N','2',NULL,current timestamp,'SQL_INSERT',current timestamp,'SQL_INSERT',0,NULL,1723,1142)
  RETURNING door_number,dock_door_sysid),
ins2 AS (
INSERT INTO SIT.DOCK_DOOR_lane (DOCK_DOOR_LANE_SYSID,DOOR_LANE_ID,DOCK_DOOR_SYSID,LANE_ID,CREATE_TS,CREATE_USERID,UPDATE_TS,UPDATE_USERID,VER_NUMBER) 
VALUES (nextval for DBF1.DOCK_DOOR_LANE_seq,door_number||''||'A',dock_door_sysid,'A',current timestamp},'SQL_INSERT',current timestamp,'SQL_INSERT',0)
  SELECT door_number,dock_door_sysid FROM DBF1.DOCK_DOOR
  RETURNING door_number,dock_door_sysid)
INSERT INTO DBF1.DOCK_DOOR_lane (DOCK_DOOR_LANE_SYSID,DOOR_LANE_ID,DOCK_DOOR_SYSID,LANE_ID,CREATE_TS,CREATE_USERID,UPDATE_TS,UPDATE_USERID,VER_NUMBER) 
VALUES (nextval for DBF1.DOCK_DOOR_LANE_seq,door_number||''||'B',dock_door_sysid,'B',current timestamp},'SQL_INSERT',current timestamp,'SQL_INSERT',0)
SELECT door_number,dock_door_sysid FROM DBF1.DOCK_DOOR;

Table 1 = DOCK_DOOR

Table 2 = DOCK_DOOR_LANE

+4
source share
1 answer

You can do this with a trigger in a table dock_door.

, IBM i. ,

.

insert into dock_door_lane
  select <....>
    from final table (insert into dock_door <...>)

, , , , , DB2 for didn ' t .

SO , , , v7.1 .

, 7.2 .

+2

Source: https://habr.com/ru/post/1671431/


All Articles