真实表

时间:2017-10-18 11:54:58

标签: oracle plsql oracle-sqldeveloper

我有下一张桌子:

CREATE TABLE my_table (
  id  VARCHAR2(20 BYTE) NOT NULL,
  name VARCHAR2(40 BYTE) NOT NULL,
  age NUMBER,
  PRIMARY KEY (id, name)
);

我想从my_table创建表集合。填写我的数据。

我试试:

DECLARE  
  type my_type IS TABLE OF my_table%ROWTYPE;
  my_data my_type := my_type();
BEGIN
  my_data := my_type('1','Jon',5);
END;

结果我得到:

ORA-06550: line 8, column 16:
PLS-00306: wrong number or types of arguments in call to 'my_type'
ORA-06550: line 8, column 16:
PLS-00306: wrong number or types of arguments in call to 'my_type'
ORA-06550: line 8, column 16:
PLS-00306: wrong number or types of arguments in call to 'my_type'

如何创建模拟我的表的数组项?

2 个答案:

答案 0 :(得分:5)

DECLARE  
  type my_type IS TABLE OF my_table%ROWTYPE;
  my_data my_type := my_type();
  idx number;
BEGIN

  my_data.extend(1);
  idx := my_data.count;
  my_data(idx).id := 1;
  my_data(idx).name := 'John';
  my_data(idx).age := 5;


END;

DECLARE  
  type my_type IS TABLE OF my_table%ROWTYPE;
  my_data my_type := my_type();
  rec  my_table%ROWTYPE;
  idx number;
BEGIN

  my_data.extend(1);
  idx := my_data.count;
  rec.id := 1;
  rec.name := 'John';
  rec.agr := 5;
  my_data(idx) := rec;


END;

答案 1 :(得分:1)

这是您在collection块中填充table类型plSQL的方法:

 DECLARE
       TYPE my_type IS TABLE OF my_table%ROWTYPE;

       my_data   my_type := my_type ();
    BEGIN
        INSERT INTO my_table
        VALUES ('1', 'Jon', 5);

        COMMIT;

       SELECT *
         BULK COLLECT INTO my_data
         FROM my_table;

       for i in 1..my_data.count
       loop
        dbms_output.put_line(my_data(i).id);
       end loop;

    END

;