简体   繁体   English

在运行于AS400、6.1的DB2中进行SQL合并

[英]SQL merge on in DB2 running on AS400, 6.1

The following query does not work in DB2 which is running on the As400. 以下查询在As400上运行的DB2中不起作用。 I've read of similar issues but am unsure how to modify my query accordingly. 我已经读过类似的问题,但是不确定如何相应地修改查询。

merge into AB27PR AB
    using (select USER, ROCOMP,REFID, QREADSTAT
             from S490JR
            where QREADSTAT =1) SR
    on
       (AB.USER= SR.USER
        AND AB.XCCOMP = SR.ROCOMP
        AND AB.XEFID = SR.REFID )
    when matched 
     and AB.XREADSTAT = 0
    then update set XREADSTAT = SR.QREADSTAT;

Since it's now known to be 6.1, we know that MERGE isn't appropriate and a straight UPDATE could be used. 既然现在知道是6.1,我们知道MERGE不适合,可以使用直接UPDATE。 Simplest might be: 最简单的可能是:

update AB27PR AB
   set AB.XREADSTAT = 1
where AB.XREADSTAT = 0
  and exists( select SR.QREADSTAT
                from S490JR SR
                where AB.USER = SR.USER
                  AND AB.XCCOMP = SR.ROCOMP
                  AND AB.XEFID = SR.REFID
                  AND SR.QREADSTAT = 1 )

Since AB.XREADSTAT will always receive a (1) value from table S490JR, it can be supplied as a constant. 由于AB.XREADSTAT将始终从表S490JR接收(1)值,因此可以将其作为常量提供。 The only requirement is that a properly matching row should 'exist' in S490JR. 唯一的要求是,正确匹配的行应在S490JR中“存在”。

A more general UPDATE that would cover conditions that aren't needed here could look like: 涵盖了此处不需要的条件的更一般的UPDATE看起来像:

update AB27PR AB
   set AB.XREADSTAT = ( select max( SR.QREADSTAT )
                          from S490JR SR
                          where AB.USER = SR.USER
                            AND AB.XCCOMP = SR.ROCOMP
                            AND AB.XEFID = SR.REFID
                            AND SR.QREADSTAT = 1 )
where AB.XREADSTAT = 0
  and exists( select SR.QREADSTAT
                from S490JR SR
                where AB.USER = SR.USER
                  AND AB.XCCOMP = SR.ROCOMP
                  AND AB.XEFID = SR.REFID
                  AND SR.QREADSTAT = 1 )

In that case, it pulls whatever value is in SR.QREADSTAT. 在这种情况下,它将提取SR.QREADSTAT中的任何值。 Of course, since the WHERE clause limits the value to (1), it's still the only possible result for the SET clause. 当然,由于WHERE子句将值限制为(1),因此它仍然是SET子句的唯一可能结果。

Also, the MAX() function is used in order to handle the possibility that there might be multiple rows in S490JR that satisfy the WHERE conditions. 另外,使用MAX()函数来处理S490JR中可能存在满足WHERE条件的多行的可能性。 (We don't know what every row in your table contains.) The result set of the sub-select of the SET clause can only contain a single row. (我们不知道表中的每一行都包含什么。)SET子句的子选择结果集只能包含一行。 Only a single value can fit into the SET column. SET列中只能包含一个值。 The MAX() function ensures a single value even though that value might be in multiple matching rows. MAX()函数可确保单个值,即使该值可能在多个匹配行中也是如此。 The MIN() function could also be used in place of MAX(). MIN()函数也可以代替MAX()使用。 (I'd like SQL to have something like an ANY() function, but the SQL standard used for 6.1 simply has nothing like that.) (我希望SQL具有ANY()函数之类的东西,但是用于6.1的SQL标准根本​​没有这样的东西。)

Note that in both examples the subselect needs to condition the WHERE clause of the UPDATE statement itself. 请注意,在两个示例中,subselect都需要条件UPDATE语句本身的WHERE子句。 You want to be sure to update only "matching" rows. 您希望确保仅更新“匹配”行。

抱歉,系统实际上正在运行不支持MERGE的OS 6.1。

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

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