简体   繁体   English

DB2 SQL更新中的案例结构

[英]CASE STRUCTURE IN A DB2 SQL UPDATE

I need to update a value (FIELD_E) in one table based on the value in a field of a second table as long as certain conditions are met. 只要满足某些条件,我就需要根据第二个表的字段中的值更新一个表中的值(FIELD_E)。 Here is what I am working with thus far. 到目前为止,这是我正在使用的东西。

TABLE_1.FIELD_C is a primary key TABLE_1.FIELD_C是主键

TABLE_2.FIELD_D is a foreign key TABLE_2.FIELD_D是外键

There is a ONE TO MANY relationship between TABLE_1.FIELD_C AND TABLE_2.FIELD_D TABLE_1.FIELD_C和TABLE_2.FIELD_D之间存在一对多的关系

UPDATE TABLE_1
SET    TABLE_1.FIELD_E = (
   CASE
          WHEN
                 (
                        SELECT MAX(FIELD_A)
                        FROM   TABLE_1,
                               TABLE_2
                        WHERE  TABLE_1.FIELD_C = TABLE_2.FIELD_D
                        AND    TABLE_1.FIELD_A IS NOT NULL
                        AND    TABLE_2.FIELD_B IS NULL) = 'Y' THEN '1'
          WHEN
                 (
                        SELECT MAX(FIELD_A)
                        FROM   TABLE_1,
                               TABLE_2
                        WHERE  TABLE_1.FIELD_C = TABLE_2.FIELD_D
                        AND    TABLE_1.FIELD_A IS NOT NULL
                        AND    TABLE_2.FIELD_B IS NULL) = 'N' THEN '10'
          WHEN
                 (
                        SELECT MAX(FIELD_A)
                        FROM   TABLE_1,
                               TABLE_2
                        WHERE  TABLE_1.FIELD_C = TABLE_2.FIELD_D
                        AND    TABLE_1.FIELD_A IS NOT NULL
                        AND    TABLE_2.FIELD_B IS NULL) = 'U' THEN '9'
          ELSE NULL
   END 
WHERE TABLE_1.FIELD_C = TABLE_2.FIELD_D
AND    TABLE_1.FIELD_A IS NOT NULL
AND    TABLE_2.FIELD_B IS NULL

I know I could probably do this more simply by just taking a snapshot and writing multiple statements using primary keys but if the values were to change after the snapshot and before the update, an incorrect update could be made. 我知道我可以更简单地做到这一点,只需拍摄快照并使用主键编写多个语句,但是如果值在快照之后和更新之前更改,则可能会进行错误的更新。

Can I get some suggestions on how to do this using a similar case structures statement? 我可以使用类似的案例结构语句获得有关如何执行此操作的一些建议吗?

Your query is doing several things you likely do not intend, besides the blatant syntax and construction errors. 除了公然的语法和构造错误之外,您的查询还在做您可能不希望做的几件事。 Making some assumptions about reasonable behavior, the query can be a lot simpler than your current effort: 对合理的行为做出一些假设,查询可能比您当前的工作简单得多:

UPDATE Table_1 SET field_e = CASE field_a WHEN 'Y' THEN '1'
                                          WHEN 'N' THEN '10'
                                          WHEN 'U' THEN '9'
                                          ELSE null END
WHERE field_a IS NOT NULL
      AND EXISTS (SELECT '1'
                  FROM Table_2
                  WHERE Table_2.field_d = Table_1.field_c
                        AND Table_2.field_b IS NULL)

SQL Fiddle Example SQL小提琴示例


Your original query had the following behavior or other problems: 您的原始查询具有以下行为或其他问题:

  1. Your subqueries were certainly returning more than one row (error) 您的子查询肯定返回了多个行(错误)
  2. Your outermost SELECT mentions Table_2 without providing a reference (error) 您最外层的SELECT提到了Table_2而没有提供参考(错误)
  3. There was no correlation between the outer UPDATE and any of the subqueries, meaning everything would be updated to have 1 , regardless of the actual contents of a particular row's field. 外部UPDATE与任何子查询之间没有关联,这意味着所有内容都将更新为1 ,而不管特定行字段的实际内容如何。

    • I'm also not sure if you want the ELSE NULL in your CASE - this has the effect of erasing whatever value is currently in field_e if field_a doesn't have a matching value. 我也不确定您是否要在CASE使用ELSE NULL field_e如果field_a没有匹配的值,则具有擦除field_a中当前值的作用。 Or rather, not restricting the update to those rows containing the values interested in (because ELSE null is implied if excluded). 或者更确切地说,不将更新限制为包含感兴趣的值的那些行(因为如果排除则暗含ELSE null )。

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

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