【问题标题】:Using db2 merge with php parameters使用带有 php 参数的 db2 合并
【发布时间】:2019-03-09 23:09:06
【问题描述】:

我有一个工作插入到 DB2,在脚本中使用 PHP 参数。它就像我想要的那样工作,但我正在尝试修复它,这样我就不必每次都截断和重建。如果我的唯一键字段已经存在,我想简单地更新我的最后 2 个字段。

插入:

INSERT INTO testSchema.metrics (cust, item, material, color, group, group2, sales, score )
    VALUES (
        :cust_id, 
        :item,
        :material,
        :color,
        :group,
        :group2,
        :sales,
        :score
    )

我对 cust_id、item、material 和 color 有一个唯一的键约束,所以我基本上是在尝试进行 UPSERT,但在 DB2 中。

我知道我可以在 DB2 中使用 MERGE,并且我已经开始使用这里,但我只是对如何充实它感到困惑,特别是因为我使用的是脚本中的参数。

这是我的伪合并:

MERGE INTO testSchema.metrics as m
USING // I guess I would need all of my parameters here?
ON :cust_id, :item, :material, :color
WHEN MATCHED THEN
    UPDATE SET sales = :sales, score = :score
WHEN NOT MATCHED THEN
    INSERT (cust, item, material, color, group, group2, sales, score )
    VALUES (
            :cust_id, 
            :item,
            :material,
            :color,
            :group,
            :group2,
            :sales,
            :score
        );

我觉得这是基本想法,但我很困惑如何修复参数的 USING 和 ON 行。

我只是想说(如果一行与 cust_id、item、material 和 color 匹配,则使用新的销售额和分数进行更新。否则,插入)

【问题讨论】:

    标签: sql merge db2


    【解决方案1】:

    试试这个:

    MERGE INTO testSchema.metrics as m
    USING (
    SELECT * FROM TABLE(VALUES 
    (:cust_id, :item, :material, :color, :group, :group2, :sales, :score)
    ) t (cust_id, item, material, color, group, group2, sales, score)
    ) t on t.cust_id=m.cust_id and t.item=m.item
    WHEN MATCHED THEN UPDATE 
    SET sales = t.sales, score = t.score
    WHEN NOT MATCHED THEN INSERT (cust_id, item, material, color, group, group2, sales, score)
    VALUES (t.cust_id, t.item, t.material, t.color, t.group, t.group2, t.sales, t.score);
    

    【讨论】:

    • 您是否在选择中使用 TABLE 作为占位符?还是您只是将其命名为 TABLE,因为它正在选择这些参数
    • 抱歉,我没有收到问题。我使用这种方式来命名结果集中的列。 SELECT * FROM TABLE( VALUES (1, 1, 1, 1, 1, 1, 1, 1), (2, 2, 2, 2, 2, 2, 2, 2) ) t (cust_id, item, material, color, group, group2, sales, score)
    【解决方案2】:

    根据IBM tutorial(在“使用值的单行合并”部分下),您只需在USING 子句中的一个位置设置参数,然后将每个参数映射到一个别名,S。然后定义MATCHEDNOT MATCHED逻辑。

    MERGE INTO testSchema.metrics AS T 
          USING (VALUES (
                         :cust_id, 
                         :item,
                         :material,
                         :color,
                         :group,
                         :group2,
                         :sales,
                         :score
                        )
                 ) 
         AS S(CUST_ID, ITEM, MATERIAL, COLOR, "GROUP", GROUP2, SALES, SCORE)
         ON S.CUST_ID = T.CUST_ID
      WHEN MATCHED 
         THEN UPDATE SET SALES = S.SALES, SCORE = S.SCORE
      WHEN NOT MATCHED 
         THEN INSERT VALUES(S.CUST_ID, S.ITEM, S.MATERIAL, S.COLOR, 
                            S."GROUP", S.GROUP2, S.SALES, S.SCORE);
    

    【讨论】:

    • 你在哪里做了ON s.cust_id = t.cust_id我可以为那个ON做3或4个条件对吗?
    • 可能。我认为它像任何ON 子句一样处理。
    • 谢谢,现在试试看能不能处理
    • 它说语句包含错误数量的值。我认为您必须为值中的每一列设置更新
    • 那么它是否有效?如果您将 8 个值绑定到准备好的语句,请检查您的 PHP 代码。我希望在您的ON 中,您在条件之间使用ANDgroup 也是DB2 reserved word,不应用作列名。尝试用双引号转义。
    猜你喜欢
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    相关资源
    最近更新 更多