I want capture and keep record those affected rows another table

痞子三分冷 提交于 2019-12-23 04:44:01

问题


I am getting an error PL/SQL: ORA-00913: too many values. What I am trying to do here, My update statement effect more than one rows. And I want capture and keep record those affected rows another table. Please help. For bellow code.

 DECLARE 
  TYPE cusRec IS TABLE OF customer%ROWTYPE INDEX BY PLS_INTEGER;
   v_cusRec  cusRec;

   BEGIN

    UPDATE customer
    SET l_optionalmail = 1
    WHERE c_customer_id IN (  SELECT c_customer_id
                          FROM     customer cus,
                                   agent age
                          WHERE cus.c_customer_id = age.c_customer_id
                          AND cus.l_optionalmail = 0
                          GROUP BY age.c_customer_id
                          HAVING MIN (age.d_movein) >=SYSDATE-1) 
      RETURNING cus.c_customer_id,cus.c_lastname,l_optionalmail
    BULK COLLECT INTO v_bif001Rec;
    DBMS_OUTPUT.PUT_LINE('Updated l_optionalmail column ' || SQL%ROWCOUNT || ' Rows.');
    FOR i IN v_bif001Rec.FIRST..v_bif001Rec.LAST LOOP

        INSERT INTO l_optionalmail_wrk(c_customer_id,c_lastname,l_optionalmail,d_created)
        VALUES (v_bif001Rec(i).c_customer_id,v_bif001Rec(i).c_lastname,v_bif001Rec(i).l_optionalmail,SYSDATE);
    END LOOP;
    END;

回答1:


The error clearly says that the columns you are returning from your update statement is more than the columns defined in your collection v_bif001Rec. See how you can successfully run your code. I created a record and then a table of the records with the same column which am returning from the update statement.

DECLARE
   TYPE cusRec IS TABLE OF employee%ROWTYPE INDEX BY PLS_INTEGER;

   v_cusRec   cusRec;

   ---List down all the columns which you are returning from the update statement 
   type emp_rec is record
   ( 
     EMPLOYEE_ID  NUMBER(5),
     FIRST_NAME   VARCHAR2(20 CHAR),
     LAST_NAME    VARCHAR2(20 CHAR)
   );

   type v_bifRec is table of emp_rec index by pls_integer;
   v_bif001Rec v_bifRec;

BEGIN
      UPDATE employee emp
         SET emp.dept_no = 10
       WHERE emp.employee_id IN (  SELECT id_e
                                  from employees  
                                   )
   RETURNING emp.employee_id, emp.first_name, emp.last_name
        BULK COLLECT INTO v_bif001Rec;

   DBMS_OUTPUT.PUT_LINE ('Updated l_optionalmail column ' || SQL%ROWCOUNT || ' Rows.');

   FOR i IN v_bif001Rec.FIRST .. v_bif001Rec.LAST
   LOOP

    --You can insert your records here. I Just displayed it.  

     dbms_output.put_line('Employee_id= '||v_bif001Rec(i).EMPLOYEE_ID ||'Employee_First_name --'||v_bif001Rec(i).FIRST_NAME||'Employee Last_name--'||v_bif001Rec(i).LAST_NAME);    

     /*INSERT INTO l_optionalmail_wrk (c_customer_id,
                                      c_lastname,
                                      l_optionalmail,
                                      d_created)
           VALUES (v_bif001Rec (i).c_customer_id,
                   v_bif001Rec (i).c_lastname,
                   v_bif001Rec (i).l_optionalmail,
                   SYSDATE); */
   END LOOP;
END;


来源:https://stackoverflow.com/questions/41001373/i-want-capture-and-keep-record-those-affected-rows-another-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!