【问题标题】:Need explanation on this code involving TABLE type collection需要对涉及 TABLE 类型集合的代码进行解释
【发布时间】:2014-02-08 10:36:38
【问题描述】:
CREATE OR REPLACE PROCEDURE multirowupdate AS
  TYPE t_record IS TABLE OF employees%ROWTYPE;
  t_record record_t;
  BEGIN

    UPDATE employees
    SET salary = salary + 10
    RETURNING first_name, salary INTO record_t;

    FOR i IN 1..record_t.count
    LOOP
      dbms_output.put_line(record_t(i).first_name);
      dbms_output.put_line(record_t(i).salary);
    END LOOP;
  END;

执行时我收到 错误:

 Error(11,7): PLS-00201: identifier 'RECORD_T' must be declared

为什么当我明确声明这是声明部分时出现此错误。

我在 HR 架构中使用员工表

【问题讨论】:

    标签: sql oracle collections types plsql


    【解决方案1】:

    您的声明似乎有些混乱。

    您声明了一个 t_record 类型,然后将其用作变量的名称?

    我想你可能想做的是:

    create or replace procedure
    MultiRowUpdate
    as
    type t_record is table of employees%rowtype;
    -- This is not correct: t_record record_t;
    record_t t_record;
    
    begin
    
        update employees
        set salary=salary + 10
        returning first_name,salary
        into record_t;
    
    for  I in 1..record_t.count 
    
        loop
    
          dbms_output.put_line(record_t(I).first_name);
          dbms_output.put_line(record_t(I).salary);
    
        end loop;
    
    end;
    

    希望对你有帮助

    【讨论】:

    • 谢谢!现在我明白了我的错误,我不应该给类似的名字。还是谢谢。
    【解决方案2】:

    变量的正确声明应如下所示:

    record_t t_record;
    

    变量前面是它的类型。但是这个更正导致你另一个错误:

    PLS-00642: local collection types not allowed in SQL statements
    

    这意味着你不能在类型声明中混合 PL/SQL 和 SQL 上下文。这个问题解决起来比较复杂。为避免重复,我不会在这里发布解决方案。相反,请阅读in the answers to this question 的解释,这似乎与您的需求非常相似。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多