DB2 for IBM iSeries: IF EXISTS statement syntax

不问归期 提交于 2019-12-01 09:24:24

DB/2 on the AS/400 does not have a conditional INSERT / UPDATE statement.

You could drop the SELECT statement by executing an INSERT directly and if it fails execute the UPDATE statement. Flip the order of the statements if your data is more likely to UPDATE than INSERT.

A faster option would be to create a temporary table in QTEMP, INSERT all of the records into the temporary table and then execute a bulk UPDATE ... WHERE EXISTS and INSERT ... WHERE NOT EXISTS at the end to merge all of the records into the final table. The advantage of this method is that you can wrap all of the statements in a batch to minimize round trip communication.

+UPDATE+

DB2 for i, as of version 7.1, now has a MERGE statement which does what you are looking for.

>>-MERGE INTO--+-table-name-+--+--------------------+----------->
               '-view-name--'  '-correlation-clause-'   

  >--USING--table-reference--ON--search-condition----------------->

     .------------------------------------------------------------------------.   
     V                                                                        |   
  >----WHEN--+-----+--MATCHED--+----------------+--THEN--+-update-operation-+-+----->
             '-NOT-'           '-AND--condition-'        +-delete-operation-+     
                                                         +-insert-operation-+     
                                                         '-signal-statement-'     

See IBM i 7.1 InfoCenter DB2 MERGE statement reference page

You can perform control-flow logic (IF...THEN...ELSE) in an SQL stored procedure. Here's sample SQL source code:

-- Warning!  Untested code ahead.
CREATE PROCEDURE libname.UPSERT_MYTABLE (
    IN THEKEY DECIMAL(9,0),
    IN NEWVALUE CHAR(10) )
LANGUAGE SQL
MODIFIES SQL DATA

BEGIN

    DECLARE FOUND CHAR(1);

    -- Set FOUND to 'Y' if the key is found, 'N' if not.
    -- (Perhaps there's a more direct way to do it.)
    SET FOUND = 'N';
    SELECT 'Y' INTO FOUND
    FROM SYSIBM.SYSDUMMY1
    WHERE EXISTS
      (SELECT * FROM MYTABLE WHERE KEY = THEKEY);

    IF FOUND = 'Y' THEN

        UPDATE MYTABLE
        SET VALUE = NEWVALUE
        WHERE KEY = THEKEY;

    ELSE

        INSERT INTO MYTABLE
          (KEY, VALUE)
        VALUES
          (THEKEY, NEWVALUE);

    END IF;

END;

Once you create the stored procedure, you call it like you would any other stored procedure on this platform:

CALL UPSERT_MYTABLE( xxx, zzz );

This slightly over complex piece of SQL procedure will solve your problem:

IBM Technote

If you want to do a mass update from another table then have a look at the MERGE statement which is an incredibly powerful statement which lets you insert, update or delete depending on the values from another table. IBM DB2 Syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!