应该重构包含DML语句的游标for循环

时间:2013-11-21 07:19:51

标签: oracle loops plsql dml

如果我编译了我的程序,我不会收到错误,但我看到了这个文本(我怎么能使用这个建议):

  

应该重构包含DML语句的游标for循环以使用BULK COLLECTION和FORALL

如果我编译了这个,我总是得分:

start:2013-11-21 08:16:54
end:2013-11-21 08:16:54

我的代码:

    CREATE OR REPLACE PROCEDURE PROC_AAA
IS
   PRAGMA AUTONOMOUS_TRANSACTION;
   company_              VARCHAR2 (2) := 'IT';
   ok_                   BOOLEAN;
   updated_              BOOLEAN;
   error_msg_            VARCHAR2 (4000);
   category_             VARCHAR2 (50);
   exist_atrribute_id_   VARCHAR2 (50);
   exist_value_          NUMBER;
   objid_                ROWID;
   var_rows              NUMBER (5);

   CURSOR cur_dane_wej
   IS
      SELECT *
        FROM dane_wejsciowe_cns
       WHERE status = 'NEW'
      FOR UPDATE;
BEGIN
   DBMS_OUTPUT.put_line (
      'start:' || TO_CHAR (SYSDATE, 'YYYY-MM-DD hh24:mi:ss'));

   FOR cur_dane_wej_read IN cur_dane_wej
   LOOP
      BEGIN
         SAVEPOINT cur_dane_wej_svp;
         updated_ := FALSE;
         error_msg_ := NULL;

         category_ := TERMINAL_API.Get_Category_Id (cur_dane_wej_read.IMEI_NO);

         exist_atrribute_id_ :=
            IFSAPP.SER_NUM_ATTRIBUTE_API.CHECK_EXIST (
               COMPANY_,
               category_,
               cur_dane_wej_read.IMEI_NO,
               cur_dane_wej_read.KOD_CECHY);

         SELECT COUNT (*)
           INTO exist_value_
           FROM IFSAPP.SER_NUM_ATTRIBUTE
          WHERE     SERIAL_NO = cur_dane_wej_read.IMEI_NO
                AND COMPANY = company_
                AND CATEGORY_ID = category_
                AND ATTRIBUTE_ID = cur_dane_wej_read.WARTOSC_CECHY
                AND NVL (VALUE, '_dummy_') =
                       NVL (cur_dane_wej_read.KOD_CECHY, '_dummy_');



         DBMS_OUTPUT.put_line (
               cur_dane_wej_read.KOD_CECHY
            || '  '
            || cur_dane_wej_read.WARTOSC_CECHY);
         DBMS_OUTPUT.put_line (
               'kategoria: '
            || category_
            || '  imei:  '
            || cur_dane_wej_read.IMEI_NO);


         IF exist_atrribute_id_ = 'TRUE'
         THEN
            SELECT objid
              INTO objid_
              FROM SER_NUM_ATTRIBUTE
             WHERE     COMPANY = company_
                   AND CATEGORY_ID = category_
                   AND SERIAL_NO = cur_dane_wej_read.IMEI_NO
                   AND ATTRIBUTE_ID = cur_dane_wej_read.KOD_CECHY;

            DBMS_OUTPUT.put_line ('objid:  ' || objid_);

            IF exist_value_ > 0
            THEN
               DBMS_OUTPUT.put_line ('--> wartosc cechy istnieje ');
               NULL;
            ELSE
               DBMS_OUTPUT.put_line ('zmodyfikowano wartosc cechy');
               updated_ := TRUE;
            END IF;
         ELSE
            DBMS_OUTPUT.put_line (
               'kod cechy nie istnieje.. trwa dodanie nowego kodu cechy');
            --                     IFSAPP.SER_NUM_ATTRIBUTE_API.SET_VALUE (company_, category_id_,cur_dane_wej_read.IMEI_NO, cur_dane_wej_read.KOD_CECHY, cur_dane_wej_read.WARTOSC_CECHY, 'DODANO NOWY KOD CECHY' );
            updated_ := TRUE;
         END IF;

         IF updated_ = TRUE
         THEN
            DBMS_OUTPUT.put_line ('aktualizacja danych w tabeli wejsciowej');

            UPDATE dane_wejsciowe_cns
               SET status = 'DONE',
                   MESSAGE = ' ',
                   DATA_PRZETWORZENIA = SYSDATE
             WHERE CURRENT OF cur_dane_wej;
         ELSE
            UPDATE dane_wejsciowe_cns
               SET status = 'SKIPPED',
                   MESSAGE = ' ',
                   DATA_PRZETWORZENIA = SYSDATE
             WHERE CURRENT OF cur_dane_wej;
         END IF;

         COMMIT;
      EXCEPTION
         WHEN OTHERS
         THEN
            ROLLBACK TO cur_dane_wej_svp;
            error_msg_ := 'Błąd:  - ' || SQLCODE || ' -ERROR- ' || SQLERRM;

            UPDATE dane_wejsciowe_cns
               SET status = 'ERROR',
                   MESSAGE = error_msg_,
                   DATA_PRZETWORZENIA = SYSDATE
             WHERE CURRENT OF cur_dane_wej;

            COMMIT;
      END;
   END LOOP;

   COMMIT;
   DBMS_OUTPUT.put_line (
      'end:' || TO_CHAR (SYSDATE, 'YYYY-MM-DD hh24:mi:ss'));
END PROC_AAA;

1 个答案:

答案 0 :(得分:0)

了解如何使用批量收藏,请查看此article