简体   繁体   中英

SQL insert into 2 tables in one query

I have the following query in SQLRPGLE for DB2:

    INSERT INTO ITEMS2 (PROGRAM, VLDFILE, VLDFLD, 
    SELFILE, SELFLD) VALUES(:SCAPP   , 'CSTMR', 'CYC',
    'BYC', 'BYCC');  

I would like this query to be run in 2 libraries as in FIRST/ITEMS2 and SECOND/ITEMS2

where FIRST and SECOND are the library names. Can this be achieved in one query?

For those who have no understanding of iSeries: The above insert statement would be similar to having a insert query for 2 tables.

The INSERT statement does not support inserting into multiple tables.

However you could create a trigger on FIRST/ITEMS2 to automatically insert/update/delete the record into SECOND/ITEMS2 .

See the CREATE TRIGGER statement for more information.

If this will be run often, consider making the INSERT into a stored procedure, and then setting the target schema via SET SCHEMA:

set schema=first;
call my_insert_proc(:scapp);
set schema=second;
call my_insert_proc(:scapp);

You could create a QMQuery like this

    INSERT INTO &LIB/ITEMS2 
             (PROGRAM, VLDFILE, VLDFLD, SELFILE, SELFLD)
      VALUES (&SCAPP, 'CSTMR',  'CYC',  'BYC',   'BYCC');  

Then

STRQMQRY myQmQry  SETVAR(('LIB' 'FIRSTLIB')('SCAPP' &VAR))
STRQMQRY myQmQry  SETVAR(('LIB' 'SECONDLIB')('SCAPP' &VAR))

From IBM's Syntax diagram of INSERT ( http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Fdb2%2Frbafzbackup.htm ), I'd say you have to go with two queries.

But after the first time of executing this query, you can try changing the current library ( http://publib.boulder.ibm.com/infocenter/iadthelp/v7r1/topic/com.ibm.etools.iseries.langref2.doc/chglibl.html ).

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