循环遍历列表并插入表中

时间:2012-02-15 13:56:24

标签: sql oracle

我想从一个表中取值并扔进另一个表中。我有功能,我正在进行批量收集到列表中。豆类清单。

     FUNCTION get_things_info ( p_part_id IN NUMBER)
  RETURN bean_list
IS
  thing_list  bean_list;
  BEGIN

  SELECT thing_bean (id, file_name, file_type, dbms_lob.getlength(thing), auditable)
  BULK COLLECT INTO thing_list
  FROM part_things
  WHERE part_id = p_part_id;

  RETURN thing_list;

  END get_things_info_by_id;

我想获取该列表,迭代它并放入具有相同数据类型的已删除表。我有一个基于一些java代码执行插入的过程:

PROCEDURE insert_thing(p_thing_bean IN OUT NOCOPY file_thing_bean, p_user_id IN NUMBER)
   IS
   BEGIN


  INSERT INTO deleted_part_things
    (id, part_id, file_name, file_type, thing, editable)
  VALUES ( p_thing_bean.id, 
          p_thing_bean.parent_id, 
          p_thing_bean.file_name, 
          p_thing_bean.file_type,
          p_thing_bean.attachment, 
          p_thing_bean.editable);

   END insert_thing;

它不必使用此过程。我只需要知道如何遍历从第一个函数返回的列表并插入deleted_part_thing

2 个答案:

答案 0 :(得分:5)

您可以使用FORALL 然后,这会迭代通过作为插入记录的参数提供的集合,我给你一个类似的例子,你可以修改以满足你的需求:

PROCEDURE insert_from_list (
   p_bean_list IN bean_list
)
IS
BEGIN
   FORALL x IN INDICES OF p_bean_list
      INSERT INTO deleted_hot_part_attachments
      (id, hot_part_id, file_name, file_type, attachment, auditable)
      VALUES (
         p_bean_list(x).id,
         p_bean_list(x).parent_id,
         p_bean_list(x).file_name,
         p_bean_list(x).file_type,
         p_bean_list(x).attachment,
         p_bean_list(x).auditable
      );
      --
      COMMIT;
END insert_from_list; 

希望它有所帮助...

编辑:如果你使用10g或更早版本,你需要将记录拉入一个集合,该集合与要插入的表格结构相同,然后调用:

FORALL x IN INDICES OF <new_collection>
   INSERT INTO deleted_hot_part_attachments
   VALUES <new_collection>(x);

因此,如果deleted_hot_part_attachments的结构与集合类型bean_list完全匹配,那么您的FORALL将是:

FORALL x IN INDICES OF p_bean_list
   INSERT INTO deleted_hot_part_attachments
   VALUES p_bean_list(x);

如果没有,那么你需要这样的东西:

PROCEDURE insert_from_list (
   p_bean_list IN bean_list
)
IS
   -- Declare collection to hold table values
   TYPE dhpa_tabtype IS TABLE OF deleted_hot_part_attachments%ROWTYPE
        INDEX BY PLS_INTEGER;
   dhpa_tab dhpa_tabtype;
BEGIN
   -- Loop through the bean list collection populating the 
   -- new dhpa_tab collection with the required values
   FOR i IN p_bean_list.FIRST .. p_bean_list.LAST
   LOOP
      dhpa_tab(i).id         := p_bean_list(i).id;
      dhpa_tab(i).parent_id  := p_bean_list(i).parent_id;
      dhpa_tab(i).file_name  := p_bean_list(i).file_name;
      dhpa_tab(i).file_type  := p_bean_list(i).file_type;
      dhpa_tab(i).attachment := p_bean_list(i).attachment;
      dhpa_tab(i).auditable  := p_bean_list(i).auditable;
   END LOOP;

   -- Populate the table using the new dhpa_tab collection values
   FORALL x IN INDICES OF dhpa_tab
      INSERT INTO deleted_hot_part_attachments
      VALUES dhpa_tab(x);

   --
   COMMIT;
END insert_from_list; 

答案 1 :(得分:0)

更老式的循环收集方法就像:

declare
  type t_tab is table of varchar2(10);
  tab t_tab;
  tab_idx pls_integer;

begin
  -- populate tab (dense at first)
  select 'val' || level
  bulk collect into tab
  from dual connect by level <= 20;

  -- make tab sparse (just for fun)
  tab.delete(3); -- remove 3rd element
  tab.extend(5); -- make space for 5 more elements
  tab(25) := 'val25';

  -- LOOP through table contents
  tab_idx := tab.first;
  loop
    exit when tab_idx is null;
    dbms_output.put_line('Element ' || tab_idx || ' is ' || tab(tab_idx));
    tab_idx := tab.next(tab_idx);
  end loop;

end;

好消息是你对forall

没有相同的限制