PL/SQL 使用对象类型函数的代码错误

PL/SQL code error for using the function from an object type

我想使用我在类型 student_typ 中声明的程序使用 PL/SQL table 打印出所有学生记录。但是好像不行。

这是我的代码:

CREATE TYPE student_typ AS OBJECT (
idno        NUMBER,
first_name  VARCHAR2(20),
last_name   VARCHAR2(25),
email       VARCHAR2(25),
phone       VARCHAR2(20),
MAP MEMBER FUNCTION get_idno RETURN NUMBER,
MEMBER PROCEDURE display_details ( SELF IN OUT NOCOPY student_typ ));

CREATE TYPE BODY student_typ AS
MAP MEMBER FUNCTION get_idno RETURN NUMBER IS
BEGIN
   RETURN idno;
END;
MEMBER PROCEDURE display_details ( SELF IN OUT NOCOPY student_typ ) IS
BEGIN
   -- use the PUT_LINE procedure of the DBMS_OUTPUT package to display details
   DBMS_OUTPUT.PUT_LINE(TO_CHAR(idno) || ' ' || first_name || ' ' || last_name);
   DBMS_OUTPUT.PUT_LINE(email || ' ' || phone);
END;
END;
CREATE TABLE student_obj_table OF student_typ;

INSERT INTO student_obj_table VALUES (
student_typ (935, 'Julie', 'Brown', 'jbrown@bentley.edu', '1-800-555-1313') ); 

INSERT INTO student_obj_table VALUES (
936, 'Julia', 'Blue', 'jblue@bentley.edu', '1-800-555-1314'); 

SELECT VALUE(s) FROM student_obj_table s
WHERE s.last_name = 'Brown';

以上代码全部正确

这是我的 PL/SQL 块:

DECLARE
student student_typ;
cursor find is select * from student_obj_table;
BEGIN -- PL/SQL block for selecting-displaying a student
  for find_rec in find loop
     student:=find_rec.student_typ;
     student.display_details();
  end loop;
END;

我想知道我的 PL/SQL 块有什么问题,它给我这个错误消息以及如何更正它:

Error report -
ORA-06550: line 6, column 24:
PLS-00302: component 'STUDENT_TYP' must be declared
ORA-06550: line 6, column 6:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

没有人 在现实生活中使用 table of object_type,但我设法让它工作。您需要使用与第一个脚本中相同的 "select value(s)" 语法。给那个别名(我用 x)然后你可以在循环中引用它:

DECLARE
student student_typ;
cursor find is select value(s) x from student_obj_table s;
BEGIN -- PL/SQL block for selecting-displaying a student
  for find_rec in find loop
    student:=find_rec.x;
    student.display_details();
  end loop;
END;
/

或者,您仍然可以使用 "select *",但您必须单独分配元素:

DECLARE
  student student_typ;
  cursor find is select * from student_obj_table s;
BEGIN -- PL/SQL block for selecting-displaying a student
  for find_rec in find2 loop
    student:= student_typ(find_rec.idno, find_rec.first_name, find_rec.last_name, find_rec.email, find_rec.phone);
    student.display_details();
  end loop;
END;
/

这是因为"select *"打断了对象的属性:

SQL> select * from student_obj_table;

      IDNO FIRST_NAME  LAST_NAME        EMAIL                     PHONE
---------- ----------- ---------------- ------------------------- ----------------
       935 Julie       Brown            jbrown@bentley.edu        1-800-555-1313
       936 Julia       Blue             jblue@bentley.edu         1-800-555-1314

但是 "select value(...)" returns 整个对象:

SQL> select value(s) x from student_obj_table s;

X(IDNO, FIRST_NAME, LAST_NAME, EMAIL, PHONE)
--------------------------------------------------------------------------
STUDENT_TYP(935, 'Julie', 'Brown', 'jbrown@bentley.edu', '1-800-555-1313')
STUDENT_TYP(936, 'Julia', 'Blue', 'jblue@bentley.edu', '1-800-555-1314')

我是这样用的,我们很多代码都是这样用的: 其中 detalleCuentas 是 detalleCuenta 类型的 table </p> <p>FOR idx IN detalleCuentas.resumenCuentas.FIRST .. detalleCuentas.resumenCuentas.LAST 循环<br> 开始 cuenta := detalleCuentas.resumenCuentas(idx); dbms_output.put_line('昆达' || idx); dbms_output.put_line('NroCuenta:' || cuenta.nroCuenta); dbms_output.put_line('CodCuenta:' || cuenta.codigoCuenta); dbms_output.put_line('TipoCuenta:' || cuenta.tipoCuenta); dbms_output.put_line('DescTipoCuenta:' || cuenta.descTipoCuenta); dbms_output.put_line(' Denominacion: ' || cuenta.denominacion); dbms_output.put_line('莫内达:'|| cuenta.codMoneda); dbms_output.put_line(' Sald o Actual:' || cuenta.saldoActual);<br> 结尾; 结束循环;