如何捕获更新列值并使用它?

Posted

技术标签:

【中文标题】如何捕获更新列值并使用它?【英文标题】:How do i capture the update column value and use it? 【发布时间】:2018-11-22 11:33:06 【问题描述】:

您好,我有一个场景,使用游标循环获取所有发票值并检查详细信息并将标志值更新为“E”,如果满足更新验证并且仅将这些发票插入另一个不满足的表更新声明。

有没有一种方法可以根据标志值插入这些发票?

请找到代码:

Procedure 
 CURSOR c2
      IS
         SELECT *
           FROM invoice_tl
          WHERE process_flag = 'N';           
BEGIN
     FOR rec IN c2
     LOOP
     BEGIN

        fnd_file.put_line (fnd_file.LOG, 'The Line Number is ' || ' ' || rec.line_number);

           IF rec.line_number IS NOT NULL
           THEN

              UPDATE invoice_tl
                 SET process_flag = 'E',
                     error_description =
                        (SELECT 'Credit Memo line amount cannot be more than Invoice Line Amount : '
                        || (rctl.extended_amount
                                    - NVL (
                                         (SELECT SUM (amount)
                                            FROM ar_activity_details
                                           WHERE customer_trx_line_id =
                                                    rctl.customer_trx_line_id),
                                         0)
                                    + NVL (
                                         (SELECT SUM (extended_amount)
                                            FROM ra_customer_trx_lines_all
                                           WHERE previous_customer_trx_line_id =
                                                    rctl.customer_trx_line_id),
                                         0))
                           FROM ra_customer_trx_all rct,
                                ra_customer_trx_lines_all rctl
                          WHERE rct.customer_trx_id =
                                   rctl.customer_trx_id
                                AND rct.org_id = 2326
                                AND rct.trx_number = rec.invoice_number
                                AND rctl.line_number = rec.line_number
                                AND rct.cust_trx_type_id =
                                       ln_trans_type_id)
               WHERE process_flag = 'N'
                     AND invoice_number = rec.invoice_number
                     AND line_number = rec.line_number
                     AND amount >
                             (SELECT (rctl.extended_amount
                                     - NVL (
                                          (SELECT SUM (amount)
                                             FROM ar_activity_details
                                            WHERE customer_trx_line_id =
                                                     rctl.customer_trx_line_id),
                                          0)
                                     + NVL (
                                          (SELECT SUM (extended_amount)
                                             FROM ra_customer_trx_lines_all
                                            WHERE previous_customer_trx_line_id =
                                                     rctl.customer_trx_line_id),
                                          0))
                               FROM ra_customer_trx_all rct,
                                    ra_customer_trx_lines_all rctl
                              WHERE rct.customer_trx_id =
                                       rctl.customer_trx_id
                                    AND rct.org_id = 2326
                                    AND rct.trx_number =
                                           rec.invoice_number
                                    AND rctl.line_number =
                                           rec.line_number
                                    AND rct.cust_trx_type_id =
                                           ln_trans_type_id);

              fnd_file.put_line (
                 fnd_file.LOG,
                 'Error Message if the CM amount more than the Invoice Line amount.');
              COMMIT;
           END IF;
        END;

        BEGIN
         fnd_file.put_line (
                 fnd_file.LOG,
                 'The Process FLag is : ' || rec.process_flag);        
           INSERT INTO second_table (
                          customer_number,
                          orig_system_cust_reference,
                          orig_system_add_reference,
                          customer_name,
                          locations,
                          inv_date,
                          creation_date,
                          inv_num,
                          balance_amount,
                          customer_trx_id,
                          customer_trx_line_id,
                          NAME,
                          term_desc,
                          term_id,
                          gl_date,
                          rec_segments1,
                          rec_segments2.....
       END;
    END LOOP;    
END

【问题讨论】:

【参考方案1】:

最好的办法是使用RETURNING INTO 子句。因此,您将定义一个数组并捕获更新行的适当部分:

declare
    type line_number_tt is table of invoice_tl.line_number%TYPE;
    line_number_array line_number_tt;
begin
    ....

    update invoice_tl 
    ...
    returning line_number bulk collect into line_number_array;

    [do stuff with the array here]
end;

如果可以的话,我真的会尝试摆脱您拥有的 select-then-loop。这是逐行处理,它被称为“slow-by-slow”是有原因的。

【讨论】:

以上是关于如何捕获更新列值并使用它?的主要内容,如果未能解决你的问题,请参考以下文章

我们如何在电子表格公式中更改列值并保持行不变?

将日期时间列值批量转换和更新为 UNIX 时间戳?

如何遍历 spark 数据集并更新 Java 中的列值?

如何使用 DB2 中表的列值更新 DB1 中表的列值?

SQL如果为true,则查看不同的列值并对其进行计数

比较列值并合并