程序使用记录类型

时间:2015-10-27 15:48:51

标签: plsql record procedure

我想创建一个返回前五个记录列表的过程。我必须使用记录类型和表类型。我做错了什么?

CREATE OR REPLACE PROCEDURE procedure_example(v_table OUT v_rec) IS
  CURSOR cur1 IS
    SELECT * FROM (
      SELECT
        type_note,
        note
      FROM dd_note ORDER BY type_note)
    WHERE rownum < 5;

  TYPE v_rec IS RECORD ( v_type_note NUMBER(2)
                       , v_note VARCHAR(30));
  TYPE v_table IS TABLE OF v_rec INDEX BY BINARY_INTEGER;

  BEGIN

    OPEN cur1;

    LOOP
      FETCH cur1 INTO v_type_note, v_note;

      dbms_output.put_line(v_type_note || '. --- ' || v_note);

      EXIT WHEN cur1%NOTFOUND;
      --enter code here
    END LOOP;

    CLOSE cur1;
  END procedure_example;

1 个答案:

答案 0 :(得分:0)

您声明类型的地方是错误的。您必须声明类型&#34; v_rec&#34;并输入&#34; v_table&#34;在此过程所属的包中,不在此过程中。这是代码的改进版本。首先,在包中声明这些类型:

 TYPE v_rec IS RECORD(
    v_type_note number(2),
    v_note varchar(30));
 TYPE v_table is table of v_rec index by pls_integer;

然后这是你的功能:

  create or replace PROCEDURE procedure_example (out_table OUT v_table) IS
     BEGIN
        select type_note, note BULK COLLECT into out_table from (select type_note, note from dd_note order by type_note) where rownum < 5
     FOR i in 1..out_table.COUNT LOOP
     dbms_output.put_line(out_table(i).v_type_note || '. --- ' || out_table(i).v_note);
     end loop;
  END procedure_example;