简体   繁体   English

DB2 for IBM iSeries:IF EXISTS语句语法

[英]DB2 for IBM iSeries: IF EXISTS statement syntax

I am familiar with Sybase which allows queries with format: IF EXISTS () THEN ... ELSE ... END IF (or very close). 我熟悉Sybase,它允许使用以下格式的查询:IF EXISTS()THEN ... ELSE ... END IF(或非常接近)。 This a powerful statement that allows: "if exists, then update, else insert". 这是一个强大的语句,允许:“如果存在,则更新,否则插入”。

I am writing queries for DB2 on IBM iSeries box. 我正在IBM iSeries盒子上为DB2编写查询。 I have seen the CASE keyword, but I cannot make it work. 我见过CASE关键字,但我无法使其工作。 I always receive the error: "Keyword CASE not expected." 我总是收到错误:“关键字CASE没有预期。”

Sample: 样品:

IF EXISTS ( SELECT * FROM MYTABLE WHERE KEY = xxx )
THEN UPDATE MYTABLE SET VALUE = zzz WHERE KEY = xxx
ELSE INSERT INTO MYTABLE (KEY, VALUE) VALUES (xxx, zzz)
END IF

Is there a way to do this against DB2 on IBM iSeries? 有没有办法在IBM iSeries上对DB2执行此操作? Currently, I run two queries. 目前,我运行两个查询。 First a select, then my Java code decides to update/insert. 首先是select,然后我的Java代码决定更新/插入。 I would rather write a single query as my server is located far away (across the Pacific). 我宁愿写一个查询,因为我的服务器位于远处(横跨太平洋)。

+UPDATE+ +更新+

DB2 for i, as of version 7.1, now has a MERGE statement which does what you are looking for. 从版本7.1开始,DB2 for i现在有一个MERGE语句,它MERGE您的需求。

>>-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 请参阅IBM i 7.1 InfoCenter DB2 MERGE语句参考页

DB/2 on the AS/400 does not have a conditional INSERT / UPDATE statement. AS / 400上的DB / 2没有条件INSERT / UPDATE语句。

You could drop the SELECT statement by executing an INSERT directly and if it fails execute the UPDATE statement. 您可以通过直接执行INSERT来删除SELECT语句,如果失败则执行UPDATE语句。 Flip the order of the statements if your data is more likely to UPDATE than INSERT . 如果您的数据比INSERT更可能UPDATE ,则翻转语句的顺序。

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. 更快的选择是在QTEMP创建一个临时表,将所有记录INSERT到临时表中,然后执行批量UPDATE ... WHERE EXISTSINSERT ... WHERE NOT EXISTS最后不存在以合并所有记录进入决赛桌。 The advantage of this method is that you can wrap all of the statements in a batch to minimize round trip communication. 此方法的优点是您可以批量包装所有语句以最小化往返通信。

You can perform control-flow logic (IF...THEN...ELSE) in an SQL stored procedure. 您可以在SQL存储过程中执行控制流逻辑(IF ... THEN ... ELSE)。 Here's sample SQL source code: 这是示例SQL源代码:

-- 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: 这个稍微过于复杂的SQL过程将解决您的问题:

IBM Technote 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. 如果你想从另一个表进行批量更新,那么看一下MERGE语句,这是一个非常强大的语句,允许你根据另一个表中的值插入,更新或删除。 IBM DB2 Syntax IBM DB2语法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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