如何通过oracle中的select语句将值插入表类型?

时间:2012-09-06 11:31:09

标签: oracle11g

我是Oracle的初学者。我创建了表类型,如下所示:

TYPE metertable IS TABLE OF pseb.metermaster.meterid%type;

我不知道如何将值插入到该表类型中。我需要将以下查询的整个结果集存储到表类型中。

select  distinct(meterid) 
from pseb.consumerfeedermetermapper 
where feederid in (select distinct (fm.FeederID) 
                   from pseb.feedermaster fm,pseb.consumerfeedermetermapper cfm
                   where fm.substationid=v_v_type
                   and cfm.feederid=fm.feederid
                   and cfm.FeederID>0)
 and meterid >0
 order by meterid;

帮助我做到这一点。

3 个答案:

答案 0 :(得分:1)

使用BULK COLLECT将数据选择为该类型的变量:

declare
  mt metertable;
begin
  select  distinct(meterid) 
  bulk collect into mt
  from pseb.consumerfeedermetermapper 
  where feederid in (select distinct (fm.FeederID) 
                     from pseb.feedermaster fm,pseb.consumerfeedermetermapper cfm
                     where fm.substationid=v_v_type
                     and cfm.feederid=fm.feederid
                     and cfm.FeederID>0)
  and meterid >0
  order by meterid;

  -- Now use mt...
end;

答案 1 :(得分:0)

我有同样的问题,这段代码帮助我:

SET SERVEROUTPUT ON
DECLARE
    TYPE t_bulk_collect_test_tab IS TABLE OF bulk_collect_test%ROWTYPE;
    l_tab     t_bulk_collect_test_tab;
    l_cursor  SYS_REFCURSOR;
BEGIN
    -- Way 1
    OPEN l_cursor FOR 'SELECT * FROM bulk_collect_test';
    FETCH l_cursor
    BULK COLLECT INTO l_tab;
    CLOSE l_cursor; 
    DBMS_OUTPUT.put_line('Dynamic FETCH  : ' || l_tab.count);


    -- Way 2
    EXECUTE IMMEDIATE 'SELECT * FROM bulk_collect_test' BULK COLLECT INTO l_tab;
    DBMS_OUTPUT.put_line('Dynamic EXECUTE: ' || l_tab.count);
END;
/

http://www.dba-oracle.com/plsql/t_plsql_dynamic.htm

答案 2 :(得分:0)

declare
type tab_type is table of consumerfeedermetermapper%rowtype;
tab_t tab_type;
begin

select  distinct(meterid) bulk collect into tab_t
from pseb.consumerfeedermetermapper 
where feederid in (select distinct (fm.FeederID) 
                   from pseb.feedermaster fm,pseb.consumerfeedermetermapper cfm
                   where fm.substationid=v_v_type
                   and cfm.feederid=fm.feederid
                   and cfm.FeederID>0)
 and meterid >0
 order by meterid;
 end;

您可以使用上述代码