使用动态更新查询更新Ledger_stat表中的天数列的值

时间:2019-07-15 16:15:12

标签: oracle plsql toad

我有一个表LEDGER_STAT_DLY,其中每个月的每一天都有列DAY_01是第1天,DAY_02,....,DAY_31 )。目前,我正在使用case语句来更新Day列的值,如下所示。


CASE DAY(V_DATE)
                WHEN 1 THEN
                UPDATE /*+ index(a LEDGER_STAT_DLY_IDX02_IN) */ LEDGER_STAT_DLY A
                    SET DAY_01 =  NVL(DAY_01,0) + NVL(V_AMOUNT,0)
                    WHERE IDENTITY_CODE =  NVL(V_IDENTITY_CODE,0)
                            AND YEAR_S =  NVL(V_YEAR_S,0)
                            AND MONTH_NO = NVL(V_MONTH,0)
                            AND CONSOLIDATION_CD =  NVL(V_CONSOLIDATION_CD,0)
                            AND FINANCIAL_ELEM_ID = NVL(V_FINANCIAL_ELEM_ID,0)
                            AND ORG_UNIT_ID   = NVL(V_ORG_UNIT_ID,0)
                            AND GL_ACCOUNT_ID = NVL(V_GL_ACCOUNT_ID,0)
                            AND COMMON_COA_ID = NVL(V_COMMON_COA_ID,0)
                            AND PRODUCT_1_ID  = NVL(V_PRODUCT_1_ID,0)
                            AND PRODUCT_ID    = NVL(V_PRODUCT_ID,0)
                            AND PRODUCT_3_ID  = NVL(V_PRODUCT_3_ID,0)
                            AND COST_TYPE_ID    = NVL(V_MEMO_GL_ACCOUNT_ID,0)
                            AND BALANCE_TYPE_CD = NVL(V_BALANCE_TYPE_CD,0);

                WHEN 2 THEN
                UPDATE /*+ index(a LEDGER_STAT_DLY_IDX02_IN) */ LEDGER_STAT_DLY A
                    SET DAY_02 =  NVL(DAY_02,0) + NVL(V_AMOUNT,0)
                    WHERE IDENTITY_CODE =  NVL(V_IDENTITY_CODE,0)
                            AND YEAR_S =  NVL(V_YEAR_S,0)
                            AND MONTH_NO = NVL(V_MONTH,0)
                            AND CONSOLIDATION_CD =  NVL(V_CONSOLIDATION_CD,0)
                            AND FINANCIAL_ELEM_ID = NVL(V_FINANCIAL_ELEM_ID,0)
                            AND ORG_UNIT_ID   = NVL(V_ORG_UNIT_ID,0)
                            AND GL_ACCOUNT_ID = NVL(V_GL_ACCOUNT_ID,0)
                            AND COMMON_COA_ID = NVL(V_COMMON_COA_ID,0)
                            AND PRODUCT_1_ID  = NVL(V_PRODUCT_1_ID,0)
                            AND PRODUCT_ID    = NVL(V_PRODUCT_ID,0)
                            AND PRODUCT_3_ID  = NVL(V_PRODUCT_3_ID,0)
                            AND COST_TYPE_ID    = NVL(V_MEMO_GL_ACCOUNT_ID,0)
                            AND BALANCE_TYPE_CD = NVL(V_BALANCE_TYPE_CD,0);

-- and so forth, I have written 31 blocks

尽管工作正常,但过程中的代码却超长。您能否建议我如何使用动态查询进行更新,以便此代码减少到更少的行/单个块,而不是31个单独的case语句。 预先感谢您的帮助!

PS:正在使用V_DATE函数从day()中提取日期。逻辑是,只要day(v_date)day_number(01, 02,03...)表的LEDGER_STAT_DLY列匹配,就相应地更新该列的值。

2 个答案:

答案 0 :(得分:1)

我同意鲍勃·贾维斯(Bob Jarvis)的回答中表达的观点,但是如果重构基础表不是您的选择,则可以使用动态SQL来完成。

在您提供的示例中,好像有一个变量(DAY_01 / DAY_02 / DAY_03 ...字段)需要在两个地方使用。因此,需要将该字段替换为静态语句的其余部分。

下面是一个示例。

首先,添加一个变量以保存目标字段名称。 (这并不是真正需要的,只是为了提高可读性)。

由于在天数1,2,3与字段DAY_01DAY_02DAY_03之间的现有转换中有序转换,因此可以在直接分配。

然后在当前使用该开关的过程主体中,可以使用EXECUTE IMMEDIATE,(通过连接或UTL_LMS这样的字符串格式化程序)将语句中的列名替换为目标DAY_01DAY_02等,其中语句中的列名。

CREATE OR REPLACE PROCEDURE UPDATE_LEDGER_STAT_DLY(V_IDENTITY_CODE NUMBER,
                                                   V_CONSOLIDATION_CD NUMBER,
                                                   V_FINANCIAL_ELEM_ID NUMBER,
                                                   V_ORG_UNIT_ID NUMBER,
                                                   V_GL_ACCOUNT_ID NUMBER,
                                                   V_COMMON_COA_ID NUMBER,
                                                   V_PRODUCT_1_ID NUMBER,
                                                   V_PRODUCT_ID NUMBER,
                                                   V_PRODUCT_3_ID NUMBER,
                                                   V_DATE DATE,
                                                   V_AMOUNT NUMBER,
                                                   V_MEMO_GL_ACCOUNT_ID NUMBER DEFAULT 0,
                                                   V_POSTINGTYPE CHAR DEFAULT 'N',
                                                   V_BALANCE_TYPE_CD NUMBER DEFAULT 0)
    IS

    V_CNT    NUMBER;
    V_D      NUMBER;
    V_DAY    CHAR(6);
    V_MONTH  CHAR(2);
    V_MO     NUMBER;
    V_YEAR_S NUMBER;

BEGIN

    IF V_POSTINGTYPE = 'N' THEN

        IF NVL(V_AMOUNT, 0) <> 0 THEN

            V_MO := (MONTH(V_DATE));
            V_MONTH := LPAD(V_MO, 2, '0');
            V_YEAR_S := (YEAR(V_DATE));
            V_D := (DAY(V_DATE));
            V_DAY := 'DAY_' || lpad(V_D, 2, '0');


            EXECUTE IMMEDIATE UTL_LMS.FORMAT_MESSAGE('UPDATE /*+ index(a LEDGER_STAT_DLY_IDX02_IN) */ LEDGER_STAT_DLY A
                           SET %s =  NVL(%s,0) + NVL(:THE_AMOUNT,0)
                           WHERE IDENTITY_CODE =  NVL(:THE_IDENTITY_CODE,0)
                              AND YEAR_S =  NVL(:THE_YEAR_S,0)
                              AND MONTH_NO = NVL(:THE_MONTH,0)
                              AND CONSOLIDATION_CD =  NVL(:THE_CONSOLIDATION_CD,0)
                              AND FINANCIAL_ELEM_ID = NVL(:THE_FINANCIAL_ELEM_ID,0)
                              AND ORG_UNIT_ID   = NVL(:ORG_UNIT_ID,0)
                              AND GL_ACCOUNT_ID = NVL(:THE_GL_ACCOUNT_ID,0)
                              AND COMMON_COA_ID = NVL(:THE_COMMON_COA_ID,0)
                              AND PRODUCT_1_ID  = NVL(:THE_PRODUCT_1_ID,0)
                              AND PRODUCT_ID    = NVL(:THE_PRODUCT_ID,0)
                              AND PRODUCT_3_ID  = NVL(:THE_PRODUCT_3_ID,0)
                              AND COST_TYPE_ID    = NVL(:THE_MEMO_GL_ACCOUNT_ID,0)
                              AND BALANCE_TYPE_CD = NVL(:THE_BALANCE_TYPE_CD,0)', V_DAY, V_DAY)
                USING V_AMOUNT, V_IDENTITY_CODE, V_YEAR_S, V_MONTH, V_CONSOLIDATION_CD,
                V_FINANCIAL_ELEM_ID, V_ORG_UNIT_ID, V_GL_ACCOUNT_ID,
                V_COMMON_COA_ID, V_PRODUCT_1_ID, V_PRODUCT_ID, V_PRODUCT_3_ID, V_MEMO_GL_ACCOUNT_ID, V_BALANCE_TYPE_CD;

        END IF;

    END IF;

END;
/

答案 1 :(得分:0)

很抱歉,但是最好的选择是标准化此表,摆脱重复的DAY_01DAY_02等字段。这将使您的工作简单得多。

显然您的表格目前看起来像

CREATE TABLE LEDGER_STAT_DLY 
  (IDENTITY_CODE      NUMBER,
   YEAR_S             NUMBER,
   MONTH_NO           NUMBER,
   CONSOLIDATION_CD   NUMBER,
   FINANCIAL_ELEM_ID  NUMBER,
   ORG_UNIT_ID        NUMBER,
   GL_ACCOUNT_ID      NUMBER,
   COMMON_COA_ID      NUMBER,
   PRODUCT_ID         NUMBER,
   PRODUCT_1_ID       NUMBER,
   PRODUCT_3_ID       NUMBER,
   COST_TYPE_ID       NUMBER,
   BALANCE_TYPE_CD    NUMBER,
   DAY_01             NUMBER,
   DAY_02             NUMBER,
   ...
   DAY_31             NUMBER);

我建议将其替换为

CREATE TABLE LEDGER_STAT_DLY 
  (IDENTITY_CODE      NUMBER,
   YEAR_S             NUMBER,
   MONTH_NO           NUMBER,
   CONSOLIDATION_CD   NUMBER,
   FINANCIAL_ELEM_ID  NUMBER,
   ORG_UNIT_ID        NUMBER,
   GL_ACCOUNT_ID      NUMBER,
   COMMON_COA_ID      NUMBER,
   PRODUCT_ID         NUMBER,
   PRODUCT_1_ID       NUMBER,
   PRODUCT_3_ID       NUMBER,
   COST_TYPE_ID       NUMBER,
   BALANCE_TYPE_CD    NUMBER,
   DAY_NUMBER         NUMBER,
   DAY_VALUE          NUMBER);

DAY_NUMBER成为一个月中的天数,因此其值为1-31。DAY_VALUE被分配了用于DAY_01,DAY_02等的值。在所有编写重复代码块以处理所有DAY_xx字段的地方,您都会意识到这种标准化结构将更容易处理。

好运。