将值从一个对象表插入另一个对象表

时间:2016-09-09 10:56:22

标签: oracle plsql

我已声明此数据类型,然后填充:

CREATE OR REPLACE TYPE CRAMER."T_CREATELINK_PORTLIST" IS TABLE OF o_CreateLink_PORTLIST;

TYPE o_CreateLink_PORTLIST AS OBJECT (
    PORTNAME                VARCHAR2(50),
    PORTID                  NUMBER,
    ISNEWPORT               NUMBER,
    SHELFNAME               VARCHAR2(50),
    SLOTNAME                VARCHAR2(50),
    BANDWIDTHNAME           VARCHAR2(50),
    ISSELECTED              NUMBER );

我想要做的是从这种类型的一个表中获取一些对象到另一个相同类型的表。像这样:

lsa_all_ports cramer.t_CreateLink_PORTLIST;
INSERT INTO lsa_filter_ports VALUES( SELECT *
      FROM TABLE(CAST(lsa_all_ports AS cramer.t_CreateLink_PORTLIST)) new
      WHERE new.bandwidthname = 'band2');

我很确定这种语法是错误的,但是有一种简单的方法吗?

其他尝试:

SELECT * INTO lsa_filter_ports
    FROM TABLE(CAST(lsa_all_ports AS cramer.t_CreateLink_PORTLIST)) new
    WHERE new.bandwidthname = 'band2';
  

ORA-06550:值不够

lsa_filter_ports cramer.t_CreateLink_PORTLIST := cramer.t_CreateLink_PORTLIST();    
INSERT INTO lsa_filter_ports 
     (SELECT * FROM TABLE(CAST(lsa_all_ports AS cramer.t_CreateLink_PORTLIST))
     WHERE bandwidthname = 'band2');
  

ORA-00942:表或视图不存在

3 个答案:

答案 0 :(得分:1)

如果我理解了一切你可以像这里一样:

declare 
  lsa_all_ports t_createlink_portlist := 
    t_createlink_portlist( o_createlink_portlist ('abc', 1, 'band1'), 
                           o_createlink_portlist ('def', 2, 'band2'));
  lsa_filter_ports t_createlink_portlist := t_createlink_portlist();
begin 
  select cast(multiset(select * 
                         from table(lsa_all_ports) 
                         where bandwidthname = 'band2') 
              as t_createlink_portlist)
    into lsa_filter_ports 
    from dual;
end;

答案 1 :(得分:0)

您最好尝试使用SELECT...INTO command来执行此操作。

  

SELECT INTO语句从一个或多个数据库中检索数据   表,并将选定的值分配给变量或集合。

如果表已经创建并且您想插入其中,那么您可以简单地执行

insert into lsa_filter_ports 
select * from o_CreateLink_PORTLIST 
WHERE bandwidthname = 'band2'

答案 2 :(得分:0)

请参阅下面的用法。

- 创建对象

 create or replace TYPE o_CreateLink_PORTLIST AS OBJECT (
        PORTNAME                VARCHAR2(50),
        PORTID                  NUMBER,
        ISNEWPORT               NUMBER,
        SHELFNAME               VARCHAR2(50),
        SLOTNAME                VARCHAR2(50),
        BANDWIDTHNAME           VARCHAR2(50),
        ISSELECTED              NUMBER );

---创建与对象相同的表

 create table lsa_filter_ports (
        PORTNAME                VARCHAR2(50),
        PORTID                  NUMBER,
        ISNEWPORT               NUMBER,
        SHELFNAME               VARCHAR2(50),
        SLOTNAME                VARCHAR2(50),
        BANDWIDTHNAME           VARCHAR2(50),
        ISSELECTED              NUMBER );

- 一种对象

CREATE OR REPLACE TYPE T_CREATELINK_PORTLIST IS TABLE OF o_CreateLink_PORTLIST;

---匿名阻止

 declare
    lsa_all_ports  t_CreateLink_PORTLIST;

    begin

    INSERT INTO lsa_filter_ports 
          SELECT *
             FROM TABLE(CAST(lsa_all_ports AS t_CreateLink_PORTLIST)) new
                 WHERE new.bandwidthname = 'band2';


    end;

收集副本

declare
    lsa_all_ports     t_CreateLink_PORTLIST:= t_createlink_portlist(o_createlink_portlist ('abc', 1, 'band1'),o_createlink_portlist ('def', 2, 'band2'));
    lsa_filter_ports  t_CreateLink_PORTLIST:= t_createlink_portlist();

    cursor cur is 
              SELECT *
             --FROM TABLE(CAST(lsa_all_ports AS t_CreateLink_PORTLIST)) new
                 FROM TABLE(lsa_all_ports) new
                 WHERE new.bandwidthname = 'band2';

   begin

   for i in cur
     loop

         lsa_filter_ports.extend;

         lsa_filter_ports(cur%rowcount) := lsa_all_ports(cur%rowcount);

         dbms_output.put_line(lsa_filter_ports(cur%rowcount).PORTNAME);

     end loop;            


    end;