0

我是甲骨文的初学者。我创建的表类型如下:

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;

帮助我做到这一点。

4

3 回答 3

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;
于 2012-09-06T11:41:22.383 回答
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

于 2014-04-02T12:36:02.707 回答
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;

你可以使用上面的代码

于 2014-04-02T16:58:59.580 回答