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

问题描述 投票:0回答:2

我想使用我在类型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', '[email protected]', '1-800-555-1313') ); 

INSERT INTO student_obj_table VALUES (
936, 'Julia', 'Blue', '[email protected]', '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:
object plsql procedure
2个回答
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;
/

或者,你仍然可以使用“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            [email protected]        1-800-555-1313
       936 Julia       Blue             [email protected]         1-800-555-1314

但“select value(...)”返回整个对象:

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

X(IDNO, FIRST_NAME, LAST_NAME, EMAIL, PHONE)
--------------------------------------------------------------------------
STUDENT_TYP(935, 'Julie', 'Brown', '[email protected]', '1-800-555-1313')
STUDENT_TYP(936, 'Julia', 'Blue', '[email protected]', '1-800-555-1314')

0
投票

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

FOR idx IN detailAccounts.Accounts.FIRST .. detailAccount.Accounts.LAST.LAST LOOP BEGIN计数:= detailAccounting account(idx); dbms_output.put_line('ACCOUNT'|| idx); dbms_output.put_line('NroCuenta:'|| cuenta.nroCuenta); dbms_output.put_line('CodCuenta:'|| cuenta.codigoCuenta); dbms_output.put_line('AccountType:'|| account.typecount); dbms_output.put_line('DescTuentaCuenta:'|| Cuenta.descTipoCuenta); dbms_output.put_line('Denomination:'|| account.denomination); dbms_output.put_line('Currency:'|| account.codMoneda); dbms_output.put_line('Sald o Actual:'|| account.SaltActual); END;结束循环;

© www.soinside.com 2019 - 2024. All rights reserved.