Oracle: вставка с использованием выбора не возвращает массовый сбор вновь вставленных идентификаторов

Имя таблицы: T_record

Id(auto increment using sequence)    Name      Age
1                                    Chitta    18
2                                    Chitta1   19
3                                    Chitta2   18
4                                    Chitta3   18

У меня есть процедура PL/SQL, которая будет вставлять записи в приведенную выше таблицу.

Insert into T_record (name, Age) 
  (select  name, age 
     from T_record 
    where Age =18) 
   returning  id bulk collect into v_newly_added_conf_input_ids;

Но массовый сбор не работает.

Как я узнаю ids, которые вставлены недавно (я хочу, чтобы он использовал те же ids в другом запросе выбора)?


person Chittaranjan Sethi    schedule 03.02.2014    source источник
comment
не работает в чем?   -  person David Aldridge    schedule 03.02.2014
comment
возврат массива идентификаторов в v_newly_added_conf_input_ids   -  person Chittaranjan Sethi    schedule 03.02.2014
comment
Да, очевидно, но вы получаете ошибку? Каковы признаки или симптомы того, что он не работает?   -  person David Aldridge    schedule 03.02.2014


Ответы (1)


Нет, вы не сможете заставить его работать таким образом. Вы можете использовать предложение returning (не массовый сбор) только тогда, когда оператор insert использует предложение values.

Вы можете использовать этот вид работы, чтобы получить эти ids:

Сначала вы заполняете коллекцию значениями, которые хотите вставить, а затем используете конструкцию forall для вставки данных и возврата id в другую коллекцию:

/* identity column is a 12c feature. In prior versions you use 
   sequences - not the main point here. Use it just to save time.
 */   
create table t1(
  t1_id number generated as identity primary key,
  name1 varchar2(31),
  age   number
) ;

Pl/SQL-блок:

declare
  /* record */
  type t_rec is record(
    name1 varchar2(32),
    age   number  -- as a side note, it's better to store 
  );              -- date of birth not the age - not that dependable.

  type t_source_list is table of t_rec;
  type t_id_list     is table of number;

  l_source_list t_source_list; -- source collection
  l_id_list     t_id_list;     -- collection we are going to put IDs into
begin

  /* data we are going to insert 
    replace this query with yours */
  select dbms_random.string('l', 7)
       , level
    bulk collect into l_source_list
   from dual
  connect by level <= 11;

  /* insert data and return IDs into l_id_list collection */
  forall i in l_source_list.first..l_source_list.last
    insert into t1(name1, age) 
       values(l_source_list(i).name1, l_source_list(i).age)
    returning t1_id bulk collect into l_id_list;

  /* print the list of new IDs. */
   for i in l_id_list.first .. l_id_list.last loop
     dbms_output.put_line('ID #' || to_char(I)||':  '||to_char(l_id_list(i)));
   end loop;  
end;
/ 

Результат:

anonymous block completed
ID #1:  1
ID #2:  2
ID #3:  3
ID #4:  4
ID #5:  5
ID #6:  6
ID #7:  7
ID #8:  8
ID #9:  9
ID #10: 10
ID #11: 11
person Nick Krasnov    schedule 03.02.2014