从过程 PLSQL 中检索用户定义的记录

Retrieving user-defined record from procedure PLSQL

我终于破解了。这是我们的学校作业:

Add another procedure to the college_package called get_class_infor. The get_class_infor procedure is used to fetch and return the class id, instructor name, course title, and department for a specific class. Pass CLASS_ID as an IN parameter. Define a user-defined record TYPE for the record variables in the procedure.

Create an anonymous block to call the get_class_infor procedure and display information about a class. The following example uses class id 01.

** Class id:     1
** Instructor:   Gunther Haas
** Course Title: Algebra I
** Department:   Mathematics

我创建了这个匿名块以确保我了解如何发送参数和检索用户定义的记录。这工作得很好。

匿名块:

DECLARE
   TYPE class_type IS RECORD (
     class_id  classes.class_id%TYPE,
     i_first   instructors.first_name%TYPE,
     i_last    instructors.last_name%TYPE,
     course    courses.title%TYPE,
     dept      sections.title%TYPE
   );
   p_rec class_type;
   PROCEDURE get_class_infor(
      p_id IN NUMBER,
      p_rec OUT class_type
   ) IS
   BEGIN
      SELECT cl.class_id, i.first_name, i.last_name, cr.title, s.title 
      INTO p_rec 
      FROM classes cl 
         JOIN instructors i ON cl.instr_id = i.instructor_id
         JOIN courses cr    ON cl.course_id = cr.course_id
         JOIN sections s    ON cr.section_code = s.section_code
         WHERE class_id = p_id;
   END get_class_infor;
BEGIN
   get_class_infor(:class_id, p_rec);
   DBMS_OUTPUT.PUT_LINE('** Class id:     ' || p_rec.class_id);
   DBMS_OUTPUT.PUT_LINE('** Instructor:   ' || p_rec.i_first || ' ' || p_rec.i_last);
   DBMS_OUTPUT.PUT_LINE('** Course Title: ' || p_rec.course);
   DBMS_OUTPUT.PUT_LINE('** Department:   ' || p_rec.dept);
END;

我被卡住的地方是正确地创建包 spec/body,或者我假设是这样。我不知道我错过了什么。这是我得到的。

包装规格:

-- Example 4_2A
CREATE OR REPLACE PACKAGE college_package IS
  TYPE class_type IS RECORD (
    class_id  classes.class_id%TYPE,
    i_first   instructors.first_name%TYPE,
    i_last    instructors.last_name%TYPE,
    course    courses.title%TYPE,
    dept      sections.title%TYPE
  );
  PROCEDURE get_class_infor (
     p_id IN NUMBER, 
     p_rec OUT class_type
  );
END college_package;

包体:

-- Example 4_2B
CREATE OR REPLACE PACKAGE BODY college_package IS
  PROCEDURE get_class_infor (
    p_id IN NUMBER,
    p_rec OUT class_type
  ) IS
  BEGIN
    SELECT cl.class_id, i.first_name, i.last_name, cr.title, s.title
    INTO p_rec 
    FROM classes cl 
      JOIN instructors i  ON cl.instr_id = i.instructor_id
      JOIN courses cr     ON cl.course_id = cr.course_id
      JOIN sections s     ON cr.section_code = s.section_code
      WHERE class_id = p_id;
  END;
END college_package;

匿名块:

-- Example 4_2C
DECLARE
  TYPE class_type IS RECORD  (
    class_id  classes.class_id%TYPE,
    i_first   instructors.first_name%TYPE,
    i_last    instructors.last_name%TYPE,
    course    courses.title%TYPE,
    dept      sections.title%TYPE
  ); 
  v_rec class_type;
BEGIN
   college_package.get_class_infor(:class_id, v_rec);
   DBMS_OUTPUT.PUT_LINE('** Class id:     ' || v_rec.class_id);
   DBMS_OUTPUT.PUT_LINE('** Instructor:   ' || v_rec.i_first || ' ' || v_rec.i_last);
   DBMS_OUTPUT.PUT_LINE('** Course Title: ' || v_rec.course);
   DBMS_OUTPUT.PUT_LINE('** Department:   ' || v_rec.dept);
END;

我收到的错误信息是:

ORA-06550: line 12, column 4:
PLS-00306: wrong number or types of arguments in call to 'GET_CLASS_INFOR'
ORA-06550: line 12, column 4:
PL/SQL: Statement ignored

我创建包的方式有问题吗spec/body?我想自己找到答案,因此不胜感激正确方向的提示。

您已在包规范中定义了记录类型。这意味着您可以在其他程序中使用它,就像您可以调用过程一样。

但是您所做的是在您的匿名块中声明第二个记录类型。它们在您看来是一样的,因为它们具有相同的名称和结构。但是对于编译器来说它们是不同的,因为它们是不同的东西。该过程需要包中定义的记录类型。

因此修复非常简单:

DECLARE
  v_rec college_package.class_type;
BEGIN
   college_package.get_class_infor(:class_id, v_rec);
   DBMS_OUTPUT.PUT_LINE('** Class id:     ' || v_rec.class_id);
   DBMS_OUTPUT.PUT_LINE('** Instructor:   ' || v_rec.i_first || ' ' || v_rec.i_last);
   DBMS_OUTPUT.PUT_LINE('** Course Title: ' || v_rec.course);
   DBMS_OUTPUT.PUT_LINE('** Department:   ' || v_rec.dept);
END;