无法在游标中获取dbms_output.put_line

时间:2019-05-06 05:16:19

标签: plsql

在从多个表中使用游标更新时我没有得到dbms_output.put_line ,除了没问题,,只有问题是在使用dbms_output时,请找到该表还添加了有关我正在使用

的等级信息
    SET SERVEROUTPUT ON;

      DECLARE
          CURSOR c_update IS
            SELECT
            b.rowid   AS id,
            a.first_name,
            a.last_name
    FROM
        customer a
        LEFT OUTER JOIN customer2 b ON a.customer_id = b.customer_id;

    TYPE t_data IS
        TABLE OF c_update%rowtype INDEX BY BINARY_INTEGER;  --loading the data 
    of cursor into table
        x_data   t_data;   --instance variable decalrations
    BEGIN
        OPEN c_update;
    LOOP
        FETCH c_update BULK COLLECT INTO x_data;
        EXIT WHEN x_data.count = 0;
        FOR i IN x_data.first..x_data.last LOOP
            UPDATE customer2
            SET
                first_name = x_data(i).first_name
            WHERE
                ROWID = x_data(i).id;

        END LOOP;

        dbms_output.put_line(x_data.first_name); --getting error in this line
    END LOOP;

       CLOSE c_update;
    EXCEPTION
       WHEN OTHERS THEN
           dbms_output.put_line('The error is ' || sqlerrm);
    END;

表级别信息


-创建文件-2019年5月7日星期二


-表CUSTOMER2的DDL

CREATE TABLE CUSTOMER2
   (    CUSTOMER_ID NUMBER(5,0), 
    FIRST_NAME VARCHAR2(100 BYTE), 
    LAST_NAME VARCHAR2(100 BYTE), 
    ADDRESS_STATE VARCHAR2(10 BYTE), 
    EMAIL_ADDRESS VARCHAR2(350 BYTE)
   ) 

Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (15,'John','Stock','ND','barl_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (1,'Teresa','Hudson','NY','bwal_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (2,'Fred','Montgomery','CA','fmont@gmail.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (3,'Lois','Lawson','WA','barl_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (4,'Alice','Perry','SC','barul_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (5,'Ralph','Montgomery','TX','ralph_mont25@gmail.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (6,'Dorothy','Armstrong','OR','abc123@abc.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (7,'Fred','Wallace','NY','walcef@testwebsite.co.uk');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (8,'Joseph','Bell','FL','baral_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (9,'Lois','Martinez','CALIF','loismar@awe.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (10,'Robert','Rice','IN','robrice123');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (12,'Joseph','Bell','OR','barul_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (5,'Ralph','Montgomery','TX','ralph_mont25@gmail.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (8,'Joseph','Bell','FL','barul_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (3,'Lois','Lawson','WA','barul_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (12,'Joseph','Bell','OR','baruwl_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (15,'John','Stock','ND','baruwl_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (1,'Teresa','Hudson','NY','baruwl_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (2,'Fred','Montgomery','CA','fmont@gmail.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (10,'Robert','Rice','IN','robrice123');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (6,'Dorothy','Armstrong','OR','abc123@abc.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (7,'Fred','Wallace','NY','wallacef@testwebsite.co.uk');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (4,'Alice','Perry','SC','baruwl_suman@outlook.com');
Insert into TBAADM.CUSTOMER2 (CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_STATE,EMAIL_ADDRESS) values (9,'Lois','Martinez','CALIF','loismar@awe.com');

和用于客户表

create table customer as select * from customer2;
update customer set EMAIL_ADDRESS = 'firsttableemail@gmail.com';

2 个答案:

答案 0 :(得分:2)

复合类型的集合变量不能像记录变量一样直接用于访问列。它应该先引用一个特定的索引,然后再引用该列。

dbms_output.put_line(x_data(i).first_name); 

尚不清楚是否要显示最后一个条目,因为您已在FOR循环之外使用了它。如果要显示每个条目,请在LOOP中移动它。如果以上内容不在循环中,则它们将不起作用,因为对i的引用将无效。

编辑

我使用您提供的数据对您的代码进行了少许更改,它似乎对我有用。查看下面的DBiddle演示。

DECLARE
     CURSOR c_update IS
     SELECT
            b.rowid   AS id,
            a.first_name,
            a.last_name
             FROM
        customer a
        LEFT OUTER JOIN customer2 b ON a.customer_id = b.customer_id;

     TYPE t_data IS
        TABLE OF c_update%rowtype INDEX BY BINARY_INTEGER;
     x_data t_data; 
    BEGIN
        OPEN c_update;
     FETCH c_update BULK COLLECT INTO x_data;
            FOR i IN x_data.first..x_data.last LOOP
          UPDATE customer2
          SET
               first_name = x_data(i).first_name
          WHERE ROWID = x_data(i).id;
          dbms_output.put_line(x_data(i).first_name);
     END LOOP;

     CLOSE c_update;
EXCEPTION
     WHEN OTHERS THEN
           dbms_output.put_line('The error is ' || sqlerrm);
END;
/

DEMO

答案 1 :(得分:1)

此代码模拟您在做什么(没有UPDATE语句,在这里无关紧要);因为我没有您的表格,所以我使用了Scott的架构。另外,我不知道您的情况下使用的外循环是什么。 BULK COLLECT不需要它(因此我将其删除)。

SQL> declare
  2    cursor c_update is
  3      select deptno, dname, loc
  4      from dept
  5      where deptno >= 20;
  6
  7    type   t_data is table of dept%rowtype index by binary_integer;
  8    x_data t_data;
  9  begin
 10    open  c_update;
 11    fetch c_update bulk collect into x_data;
 12    close c_update;
 13
 14    for i in x_data.first .. x_data.last loop
 15      dbms_output.put_line(x_data(i).dname);
 16    end loop;
 17  end;
 18  /
RESEARCH
SALES
OPERATIONS

PL/SQL procedure successfully completed.

SQL>

[编辑:加入游标声明示例]

如果涉及到联接,为什么它不起作用?

SQL> create table t_update
  2    (deptno number, dname varchar2(10), loc varchar2(10), ename varchar2(10));

Table created.

SQL>
SQL> declare
  2    cursor c_update is
  3      select d.deptno, d.dname, d.loc, e.ename
  4      from dept d left join emp e on e.deptno = d.deptno
  5      where d.deptno >= 20
  6      order by d.deptno;
  7
  8    type   t_data is table of t_update%rowtype index by binary_integer;
  9    x_data t_data;
 10  begin
 11    open  c_update;
 12    fetch c_update bulk collect into x_data;
 13      close c_update;
 14
 15    for i in x_data.first .. x_data.last loop
 16      dbms_output.put_line(x_data(i).dname ||' - '|| x_data(i).ename);
 17    end loop;
 18  end;
 19  /
RESEARCH - SMITH
RESEARCH - ADAMS
RESEARCH - JONES
RESEARCH - SCOTT
RESEARCH - FORD
SALES - JAMES
SALES - TURNER
SALES - MARTIN
SALES - WARD
SALES - ALLEN
SALES - BLAKE
OPERATIONS -

PL/SQL procedure successfully completed.

SQL>