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

时间:2016-03-08 16:32:06

标签: object plsql procedure

我想使用我在类型student_typ中声明的过程,使用PL / SQL表打印出所有学生记录。但它似乎没有用。

这是我的代码:

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:

2 个答案:

答案 0 :(得分:1)

没有人在现实生活中使用object_type表,但我设法让这个工作。您需要使用相同的"选择值"您在第一个脚本中使用的语法。给它一个别名(我使用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;
/

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

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;
/

这是因为"选择*"打破对象的属性:

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

但是"选择值(...)"返回整个对象:

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')

答案 1 :(得分:0)

我使用这种方式,并且我们使用了大量的代码: 其中detalleCuentas是detalleCuenta类型的表

FOR idx IN detalleCuentas.resumenCuentas.FIRST .. detalleCuentas.resumenCuentas.LAST LOOP
BEGIN cuenta := detalleCuentas.resumenCuentas(idx); dbms_output.put_line(' CUENTA ' || 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(' Moneda: ' || cuenta.codMoneda); dbms_output.put_line(' Sald o Actual: ' || cuenta.saldoActual);
END; END LOOP;